sed remove css comments

Is there a way that I can use sed to remove lines with css comments like this?
/* comment */

Test this command whether it works

sed "/\/\*.*\*\//d;/\/\*/,/\*\// d" file

Perfect, thanks.

---------- Post updated at 12:05 PM ---------- Previous update was at 11:37 AM ----------

What about comments like this?
<!-- Comment -->

try this,

sed "/<\!.*>/ d" infile

Works great, thanks again.

Can comments and "code" be mixed on the same line?

i.e.

$ cat file1
some stuff
<!-- some comment-->
some more stuff <!-- some comment 2-->
<!-- some comment 3-->yet some more stuff

$ sed "s/<!--[^>]*>//;/^$/d" file1
some stuff
some more stuff 
yet some more stuff

The only thing I don't like about that solution is it removes all the blank lines in the file also.

Good point. An oversight, sorry :o

A tweak to scottn's solution which does not consume blank lines, handles lines with multiple comments, and strongly quotes the exclamation point to protect it from history expansion on shells that support that feature:

$ cat data
some stuff
<!-- some comment-->

some more stuff <!-- some comment 2-->
<!-- some comment 3-->yet some more stuff
<!-- some comment 4-->yet even some more stuff<!-- some comment 5-->
$ sed -e '/^$/b' -e 's/<!--[^>]*>//g; /^$/d' data
some stuff

some more stuff 
yet some more stuff
yet even some more stuff

Caveat: This only works for simple single-line comments which do not contain a ">" (which, if I recall correctly, is allowed).

Regards,
Alister

1 Like

Very nicely done :b: