I have the following sed command in my script

sed -i "/$PARAM/s/$OLD/$BEW/g"           $FILE

this sed command replace the OLD value to new in the line that PARAMETER exsit

but I need also to ignore the "#" lines
how it can possible to change the sed syntax in order to ignore "#" lines? the solution must be only with sed not by perl!

  • Any particular reason for rejecting Perl? (perl -pe "next if /^#/; s/$OLD/$NEW/ if /$PARAM/") – grawity May 27 '10 at 13:06
  • in some case it not work for example on @ string – yael May 27 '10 at 13:30
  • for example perl -pe "next if /^#/; s/@d&/new_value/ if /param5/" this not replace the @d& with new_value and its work with sed if you have solution for the perl problem please I will hope that U have answer for that THX – yael May 27 '10 at 13:33

I think you want something like this:


replace $OLD by $NEW, if there's no hash mark before $PARAM


The b (branch) command should help:

/^#/b; /$PARAM/ s/$OLD/$NEW/g

From the manual of sed:

        b label
              Branch to label; if label is omitted, branch to end of script.

  • please explain how to add the /^#/b; to my:sed -i "/$PARAM/s/$OLD/$BEW/g" ?? – yael May 27 '10 at 13:07
  • @yael: I already did. It's in the second line of my answer. – grawity May 27 '10 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.