Sed replacement does not work when using variables

Something strange happens when you try to replace the string with sed. It works:

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

Therefore, it works when I manually type lines. But in the case below, the replacement does not occur:

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

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

I am trying to replace all occurrences of a template string with a replacement string in all files in my configuration directory.

+5
source share
2 answers

Try

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

instead of this. "Quotations do not expand variables.

+16
source

Not sure if I got this right, but if you want to replace $ {PATTERN} with $ {REPLACEMENT} literally, you need to avoid the dollar and possibly the brackets, these are reserved characters in regular expressions:

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

Source: https://habr.com/ru/post/1341832/


All Articles