开发者

Problem with quoting and sed

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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜