Sed: regular expression match lines without <!--

Posted by sixtyfootersdude on Stack Overflow See other posts from Stack Overflow or by sixtyfootersdude
Published on 2010-04-05T17:28:03Z Indexed on 2010/04/05 17:33 UTC
Read the original article Hit count: 130

Filed under:
|
|

I have a sed command to comment out xml commands

sed 's/^\([ \t]*\)\(.*[0-9a-zA-Z<].*\)$/\1<!-- Security: \2 -->/' web.xml 

Takes:

<a>

  <!-- Comment -->
  <b>
    bla
  </b>

</a>

Produces:

<!-- Security: <a> -->

  <!-- Security: <!-- Comment --> -->    // NOTE: there are two end comments.  
  <!-- Security: <b> -->
    <!-- Security: bla -->
  <!-- Security: </b> -->

<!-- Security: </a> -->

Ideally I would like to not use my sed script to comment things that are already commented.

Ie:

<!-- Security: <a> -->

  <!-- Comment -->
  <!-- Security: <b> -->
    <!-- Security: bla -->
  <!-- Security: </b> -->

<!-- Security: </a> -->

I could do something like this:

sed 's/^\([ \t]*\)\(.*[0-9a-zA-Z<].*\)$/\1<!-- Security: \2 -->/' web.xml
sed 's/^[ \t]*<!-- Security: \(<!--.*-->\) -->/\1/' web.xml

but I think a one liner is cleaner (?)

This is pretty similar: http://stackoverflow.com/questions/436850/matching-a-line-that-doesnt-contain-specific-text-with-regular-expressions

© Stack Overflow or respective owner

Related posts about pattern-matching

Related posts about regex