1

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!

bronzehedwick
  • 2,862
  • 2
  • 22
  • 28
  • The whole reason to use an alternate delimiter like `,` is so that you don't need to escape the forward slashes. `s,/\*[^*]*\*+([^/*][^*]*\*+)*/,,g`. That said, the original appears to work fine for a simple input like `/* foo */`. – chepner Feb 26 '18 at 21:34
  • Thanks @chepner. Yeah I was messing around a lot, please excuse the delimiters. My testfile was normalize.css, so I'll update the question with that. – bronzehedwick Feb 26 '18 at 21:38
  • it'd help to create a small sample (say 5-10 lines) and show which lines to delete... single line comments? multiline comments? sed by default works only one line at a time (newline is default separator)... may be `sed '\#^/\*#,\#\*/$#d'` is the one you are looking for? – Sundeep Feb 27 '18 at 04:26

1 Answers1

1

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.

Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
  • Hi Tamas, thanks for the rely. I'm aware of css minifiers and pre-processors, and use them frequently. I was looking for a (hopefully) simple shell-based solution for a small use-case where I'd like to remove as many dependencies as possible. – bronzehedwick Feb 27 '18 at 15:49
  • I'd go with php cli or a perl one-liner then. Since `sed` is line oriented, this is waaay more difficult than it should be. – Tamas Rev Feb 27 '18 at 18:10
  • gotcha. I accept the answer "this is not a good idea". thanks! – bronzehedwick Feb 28 '18 at 23:38