views:

58

answers:

3

Hi!

This is how my text (html) file looks like
    <!--
     |                                |
     |  This is a dummy comment       |
     |      please delete me          |
     |         asap                   |
     |                                |
      ________________________________
     | -->

    this is another line 
    in this long dummy html file...
    please do not delete me

I'm trying to delete the comment using sed :

cat file.html | sed 's/.*<!--\(.*\)-->.*//g'

It doesn't work :( What am I doing wrong?

Thank you very much for your help!

+1  A: 

I think you can do this with awk if you want. Start:

[~] $ more test.txt
<!--

An HTML style comment 

-->

Some other text

<div>
<p>blah</p>
</div>

<!-- Whoops
     Another comment -->
<span>Something</span>

Result of the awk:

[~]$ cat test.txt | awk '/<!--/ {off=1} /-->/ {off=2} /([\s\S]*)/ {if (off==0) print; if (off==2) off=0}'
Some other text

<div>
<p>blah</p>
</div>

<span>Something</span>
eldarerathis
+3  A: 

One problem with your original attempt is that your regex only handles comments that are entirely on one line. Also, the leading and trailing ".*" will remove non-comment text.

You would better off using existing code instead of rolling your own.

http://sed.sourceforge.net/grabbag/scripts/strip_html_comments.sed

#! /bin/sed -f
# Delete HTML comments
# i.e. everything between <!-- and -->
# by Stewart Ravenhall <[email protected]>

/<!--/!b
:a
/-->/!{
    N
    ba
}
s/<!--.*-->//

(from http://sed.sourceforge.net/grabbag/scripts/)

See this link for various ways to use perl modules for removing HTML comments (using Regexp::Common, HTML::Parser, or File::Comments.) I am sure there are methods using other utilities.

http://www.perlmonks.org/?node_id=500603

patrickmdnet
+1  A: 

patrickmdnet has the correct answer. Here it is on one line using extended regex:

cat file.html | sed -e :a -re 's/<!--.*?-->//g;/<!--/N;//ba'

Here is a good resource for learning more about sed. This sed is an adaptation of one-liner #92

http://www.catonmat.net/blog/sed-one-liners-explained-part-three/

Brian Clements
Thanks Brian! You're great :) what does the :a mean in your sed command?
Samantha
It creates a branch label named 'a'. The '//ba' at the end is branching to 'a'.
Brian Clements
Is the `//` before `ba` necessary? I don't need it in GNU `sed`.
Dennis Williamson
The double slash is short-hand for the previous expression (which is /<!--/). It is what determines if the branch will be taken (to go back and grab more lines into the buffer if it needs to). I would guess that without it, the branch is always taken and the whole file is read into one buffer. Might be an issue with a very large file, I'm not sure.
Brian Clements