Tutorial :using sed to replace whole word containing '='?



Question:

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 change 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.


Solution:1

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  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »