Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed replacement not working when using variables [duplicate]

something strange is happening when trying to replace string with sed. This works :

find /home/loni/config -type f -exec sed -i 's/some_pattern/replacement/g' {} \;

So it works when I manually type the strings. But in the case below replacement doesn't occur :

find /home/loni/config -type f -exec sed -i 's/${PATTERN}/${REPLACEMENT}/g' {} \;

When I echo these two variables PATTERN and REPLACEMENT they have the correct values.

I'm trying to replace all occurences of pattern string with replacement string in all files in my config directory.

like image 211
London Avatar asked Mar 01 '11 15:03

London


2 Answers

Try

find /home/loni/config -type f -exec sed -i "s/${PATTERN}/${REPLACEMENT}/g" {} \;

instead. The ' quotes don't expand variables.

like image 90
Erich Kitzmueller Avatar answered Oct 14 '22 07:10

Erich Kitzmueller


Not sure if I got this right, but if you want to replace the ${PATTERN} with ${REPLACEMENT} literally you have to escape the dollar and maybe the braces, those are reserved characters in regular expressions:

find /home/loni/config -type f -exec sed -i -e 's/\$\{PATTERN\}/\$\{REPLACEMENT\}/g' {} \;
like image 32
Bernhard Avatar answered Oct 14 '22 07:10

Bernhard