Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with quoting and sed

Tags:

sed

quotes

cygwin

I'm trying to find and replacing some text using sed. Specifically, I'm trying to add quotes around a variable which could contain whitespaces.

sed -i 's/$VAR some.file/"$VAR" some.file/g' path/to/file

Unfortunately, the result is not expected. I've also tried to backslash the quotes, with no luck. What am I missing here?

The sed command is executed under Windows/cygwin.

like image 735
Roberto Aloi Avatar asked Oct 15 '25 09:10

Roberto Aloi


1 Answers

You're missing the fact that single quotes prevent variable substitution:

sed -i "s/$VAR some.file/\"$VAR\" some.file/g" path/to/file

or even

sed -i $(printf 's/%s some.file/"%s" some.file/g' "$VAR" "$VAR") path/to/file
like image 97
glenn jackman Avatar answered Oct 18 '25 02:10

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!