bronzehedwick
bronzehedwick

Reputation: 3013

sed command to strip CSS comments not working

I'm trying to strip comments from a CSS file using sed. I'm using macOS, and normalize.css as a test file.

So far, I have this:

sed -E 's,\/\*[^*]*\*+([^/*][^*]*\*+)*\/,,g' < normalize.css > normalize.min.css

This does not work whatsoever; all the comments remain in the resulting normalize.min.css file.

I'm taking the regex from the CSS comments spec.

I've also tried it without substitute:

sed -E '/\/\*[^*]*\*+([^/*][^*]*\*+)*\//d' < normalize.css > normalize.min.css

With no luck.

Any help would be very much appreciated. Thanks!

Upvotes: 1

Views: 265

Answers (1)

Tamas Rev
Tamas Rev

Reputation: 7166

This task is doable with regexes. However, if you use a line-oriented tool, then it becomes unnecessarily difficult to do. This task is yelling at me, like accidental complexity!

I wouldn't push this any further. Here is an npm module for this so you can add it to your builds. Here is an online css minifier so you can use it ad-hoc.

I don't know what kind of site you're building. However, a css preprocessor might simplify your work anyway. Here is a good overview.

Upvotes: 1

Related Questions