Reputation: 412
I have some configuration files(and scripts) which I want to edit with a bash script:
.
.
.
# ... foo ...
foo
.
.
.
I used this:
sed -i 's/foo/bar/'
but as expected it will edit foo in comment line too, how can I prevent that?
Upvotes: 0
Views: 106
Reputation: 20970
This should do it:
sed -i '/^ *#/! s/foo/bar/' filename
explanation (in addition to your original sed
command):
s
command can take addresses. /^ *#/
Implies any line starting with *#
!
after the address negates its effect.So all lines NOT starting with *#
are affected.
Upvotes: 4