SED replacing content with single and double quotes - shell

I am trying to write a shell script to find a replace this line of code on Ubuntu 14.04.
//Code before script runs
/*define('FS_METHOD','direct');*/
//Code after script runs
define('FS_METHOD','direct');
sudo sed -i "s#/*define('FS_METHOD','direct');*/#define('FS_METHOD,'direct');#g" /home/example/example.txt
After running this line of code I do not get any errors, but it does not remove the /* */ I believe this is because of the single quotes. I have tried escaping them with a backslash but it doesn't work either. Anyone have any idea how to get this to work?

No, the problem is that * is an RE metacharcter so you need to escape it to have it treated literally:
$ sed "s#/\*define('FS_METHOD','direct');\*/#define('FS_METHOD,'direct');#g" file
define('FS_METHOD,'direct');
or more concisely:
$ sed "s#/\*\(define('FS_METHOD','direct');\)\*/#\1#" file
define('FS_METHOD','direct');

Related

Add string with backslash in file using sed command

I need to add a text string:
--with-mpm=event \
to httpd.spec on 138th position.
I tried:
sed -i '138i--with-mpm=event \\' /root/rpmbuild/SPECS/httpd.spec
This code runs in bash script of Vagrantfile during bootstrapping virtual machine. However, the script returns error.
When I check httpd.spec, output is a string missing backslash:
--with-mpm=event
It works okay running it directly in shell of virtual machine though.
How can I fix it with sed?
Thanks!
The rule of thumb when dealing with backslashes is keep adding up backslashes until you get the expected result.
In this case, a literal backslash here needs to be coded with four backslashes:
sed -i '138i--with-mpm=event \\\\' /root/rpmbuild/SPECS/httpd.spec

Getting issue in substituting multiple files from sed

I want to use sed for replacing multiple files from bash script.
When I call it from bash I get below error
DEBUG FLOW:-
FILELIST='/tmp/components/ab.sql /tmp/b.sql'
+ SUBSTITUTE_STRING=abc
+ sed -i.bak -e s/abc/xyz/g '/tmp/components/ab.sql /tmp/b.sql': No such file or directory
however when I used this command directly on terminal it executes successfully
sed -i.bak -e s/abc/xyz/g /tmp/components/ab.sql /tmp/b.sql
The difference from terminal and script is of quotes around the file.
I have tried defining File list variable without quotes as well
kindly suggest
Instead of saying:
FILELIST='/tmp/components/ab.sql /tmp/b.sql'
make it an array by saying:
FILELIST=(/tmp/components/ab.sql /tmp/b.sql)
and while invoking say:
sed -i.bak -e "s/abc/xyz/g" "${FILELIST[#]}"
If you look at the debug flow, it'd be evident that the shell parses the filenames as a single token ('/tmp/components/ab.sql /tmp/b.sql') which causes the No such file or directory error.

SED command not being run from bash script

I have written a bash script which calls a sed command (amongst other things) on a file to complete a find/replace of 2 different strings.
The trouble is, after running the script, I check the files and nothing has been updated. However, if I run the commands that are being produced (I echo them as output anyway) then they work.
For example, inside the script I have:
echo "/usr/local/bin/sed -i -e 's/${String1}/${String1R}/g;s/\/${String2}\//\/${String2R}\//g' ${ROOT_DIR}/data/file.sql"
/usr/local/bin/sed -i -e 's/${String1}/${String1R}/g;s/\/${String2}\//\/${TString2R}\//g' ${ROOT_DIR}/data/file.sql
Running the script does not change file.sql; however, if I run the command that is printed to console e.g. /usr/local/bin/sed -i -e 's/file_name1/file_name2/g;s//path_substring1///path_substring2//g' /path/to/file/file.sql it works perfectly!
Use double quotes instead of single quotes. Single quotes would prevent variable expansion.
/usr/local/bin/sed -i -e "s/${String1}/${String1R}/g;s/\/${String2}\//\/${TString2R}\//g" ${ROOT_DIR}/data/file.sql
Moreover, it seems that your variables are path strings which might contain forward slashes, i.e. /. In that event use a different separator:
"s|${String1}|${String1R}|g"
Using a different separator would obviate the need of escaping / in the pattern and replacement.

Bash sed implementation replace with semi-colon

I'm trying to automate an install script for New Relic and in my bash file I have the following:
_APPNAME="Test Application"
_OLD=";newrelic.appname = \"PHP Application\""
_NEW="newrelic.appname = \"${_APPNAME}\""
sed -i 's/$_OLD/$_NEW/g' /etc/php.d/newrelic.ini
For some reason that sed command doesn't trigger at all, can anyone see anything wrong with this logic?
Note I have also tried ${_OLD} and ${_NEW} to no avail.
$_OLD and $_NEW are not expanded inside single quotes. '
Use double quotes " instead:
sed -i "s/$_OLD/$_NEW/g" /etc/php.d/newrelic.ini

Sed not working inside bash script

I believe this may be a simple question, but I've looked everywhere and tried some workarounds, but I still haven't solved the problem.
Problem description:
I have to replace a character inside a file and I can do it easily using the command line:
sed -e 's/pattern1/pattern2/g' full_path_to_file/file
But when I use the same line inside a bash script I can't seem to be able to replace it, and I don't get an error message, just the file contents without the substitution.
#!/bin/sh
VAR1="patter1"
VAR2="patter2"
VAR3="full_path_to_file"
sed -e 's/${VAR1}/${VAR2}/g' ${VAR3}
Any help would be appreciated.
Thank you very much for your time.
Try
sed -e "s/${VAR1}/${VAR2}/g" ${VAR3}
Bash reference says:
The characters ‘$’ and ‘`’ retain their special meaning within double quotes
Thus it will be able to resolve your variables
I use a script like yours... and mine works as well!
#!/bin/sh
var1='pattern1'
var2='pattern2'
sed -i "s&$var1&$var2&g" *.html
See that, mine use "-i"... and the seperator character "&" I use is different as yours.
The separator character "&" can be used any other character that DOES NOT HAVE AT PATTERN.
You can use:
sed -i "s#$var1#$var2#g" *.html
sed -i "s#$var1#$var2#g" *.html
...
If my pattern is: "test#email.com" of course you must use a seperator different like "#", "%"... ok?

Resources