Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I correctly use variables with sed?

I need to use a string variable in a sed command. My attempt is given in script.sh, it does not do what I want, I assume because my variable contains characters that I need sed to evaluate. I am working in linux bash.

input.txt

delicious.banana
gross.apple

script.sh

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

adjectives="delicious\|gross\|bearable\|yummy"
sed "s/\($adjectives\)\.//g" input.txt > output.txt

output.txt desired

banana
apple

output.txt current

deliciousbanana
grossdapple

>Solution :

Non-gnu sed don’t work with \| in BRE (basic regex mode). I suggest using ERE (Extended regex mode) using -E and any escaping:

adjectives="delicious|gross|bearable|yummy"
sed -E "s/($adjectives)\.//g" input.txt

banana
apple
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading