开发者

using sed to replace whole word containing '='?

开发者 https://www.devze.com 2022-12-25 06:31 出处:网络
EDIT i have something like this in a file: imagecolor=0 arrayimagecolorcopy=0 arrayimagecolorcopy3d=0

EDIT

i have something like this in a file:

imagecolor=0

arrayimagecolorcopy=0

arrayimagecolorcopy3d=0

when i use sed -i -e 's/imagecolor=0/imagecolor=1/' it will chan开发者_如何学Goge 1 and 2 line. But i only want it to replace first line.

i also tried sed with \< \ > and \b \b, but no luck. Could it be the '=' sign? Do we have something like -w as in grep command?

Thank you.


It seems to work for me:

$ echo 'imagecolor=0
> imagecolorcopy=0
> imagecolorcopy3d=0' > input.txt

$ sed -i -e 's/imagecolor=0/imagecolor=1/' input.txt

$ cat input.txt
imagecolor=1
imagecolorcopy=0
imagecolorcopy3d=0

If you only want to make the substitution when the entire line matches, try anchoring your regular expression:

$ sed -i -e 's/^imagecolor=0$/imagecolor=1/' input.txt
0

精彩评论

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

关注公众号