开发者

replace text with a variable

I want to replace the value with a variable. The followin开发者_如何转开发g does not work because of the single quote used in sed.

#!/bin/sh
set myvarM='pqr'
sed 's/:P03:M15/:P02:M1$myvarM/' mychange.txt > new_mychange.txt

Can I change the sed command or should I use something else?


#!/bin/sh
myvarM='pqr'
sed "s/:P03:M15/:P02:M1$myvarM/" mychange.txt > new_mychange.txt

Incidentally, to make the replacement in-place (ie not create a new file, but alter the original file), do this:

sed -i '' "s/:P03:M15/:P02:M1$myvarM/" mychange.txt

This says "use a blank as the increment suffix" - ie write out the same filename as the input


awk -v val="$myvarM" '{sub(/:P03:M15/, ":P02:M1" val); print}' filename

Note that in a bourne-type shell ("/bin/sh") the set command sets the positional parameters. Your first line sets $1 to the value myvarM='pqr' -- the myvarM variable continues to be unset.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜