Drew Drew - 1 year ago 43
Bash Question

SED replacing content with single and double quotes

I am trying to write a shell script to find a replace this line of code on Ubuntu 14.04.

//Code before script runs

//Code after script runs

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

or more concisely:

$ sed "s@/\*\(define('FS_METHOD','direct');\)\*/@\1@" file