开发者

Sed replacement not working when using variables [duplicate]

This question already has answers here: Replace a string in shell script using a variable (12 answers) Closed 9 years ago.

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.


Try

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

instead. The ' quotes don't expand variables.


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' {} \;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜