Take the 2-minute tour ×
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!

share|improve this question
perhaps: /<!--[.\s\S]*?-->/ –  drudge Oct 29 '10 at 21:13
@jnpcl: That looks like Perl. It won't work in sed. –  Dennis Williamson Oct 30 '10 at 1:50
The usual warnings apply. –  Dennis Williamson Oct 30 '10 at 1:59
@Dennis: It's what worked for me with RegExPal, I didn't realize that sed used a different syntax for regular expressions. –  drudge Nov 1 '10 at 19:16
add comment

3 Answers

up vote 3 down vote accepted

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


share|improve this answer
Thanks Brian! You're great :) what does the :a mean in your sed command? –  Zenet Oct 29 '10 at 21:39
It creates a branch label named 'a'. The '//ba' at the end is branching to 'a'. –  Brian Clements Oct 29 '10 at 22:06
Is the // before ba necessary? I don't need it in GNU sed. –  Dennis Williamson Oct 30 '10 at 1:49
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 Oct 30 '10 at 2:16
add comment

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.


#! /bin/sed -f
# Delete HTML comments
# i.e. everything between <!-- and -->
# by Stewart Ravenhall <stewart.ravenhall@ukonline.co.uk>


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


share|improve this answer
add comment

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

[~] $ more test.txt

An HTML style comment 


Some other text


<!-- Whoops
     Another comment -->

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


share|improve this answer
add comment

Your Answer


