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

sed: bad flag in substitute command: '}'

I’m trying to run a command on macos terminal. The command is to create a .txt file put a string inside of it and then replace a word inside of the .txt file.

I keep getting the error: sed: bad flag in substitute command: '}'

The commands

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

echo "This is a test file" > test2.txt
sed '/test/{s/test/test2/g}' test2.txt

I also tried:

echo "This is a test file" > test2.txt
sed '/test/\\{s#test#test2#g\\}' test2.txt

But the error here was: sed: 1: "/test/\\{s#test#test2#g\\}": invalid command code \
My assumption is I need to escape the characters but I’m unsure how.

>Solution :

Commands ends with a newline or with ;.

sed '/test/{s/test/test2/g;}'

or

sed '/test/{s/test/test2/g
}'

but just:

sed '/test/s/test/test2/g'

Fun fact: empty regex repeats last regex, so you can just:

sed '/test/s//test2/g'

or really the address is not needed, just:

sed 's/test/test2/g'
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