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!

3
  • 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 */. Commented Feb 26, 2018 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. Commented Feb 26, 2018 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? Commented Feb 27, 2018 at 4:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
gotcha. I accept the answer "this is not a good idea". thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.