Drew Drew - 7 months ago 19
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
/*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?

Answer

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');