Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SED to replace long string - but got unterminated substitute in regular expression

hi trying to replace the following string with a long one :

@x@ 

with string that I got from the command line:

read test    
sed -i --backup 's/@x@/'${test}'/g' file.json README.md

but it is working only for 1 word, it is not working if there is space between word . even between quotes

sed: 1: "s/@x@/string test string: unterminated substitute in regular expression
like image 590
Tuz Avatar asked Jan 23 '26 20:01

Tuz


1 Answers

if case you run it on MacOS and struggling with "unterminated substitute in regular expression", there is an easier explanation for this:

MacOS has slightly other version of sed than usually is on linux. -i requires a parameter. If you have none, just add "" after -i

sed -i "" --backup 's/@x@/'${test}'/g' file.json README.md

or for example if you just have to delete dome line, this works on linux, but brings “invalid command code” on MacOS

sed -i 39d filenamehere.log

and this works on MacOS

sed -i "" 39d filenamehere.log
like image 189
python_kaa Avatar answered Jan 26 '26 13:01

python_kaa