This was just too painful not to blog about. I hope this saves someone else some pain of reading sed documentation to put together a pattern to match C style header comments.
In my case I’m putting together a build script, part of it is to minify Javascript before deployment.
The source file is in the format:
/** * My comment * * Description ... * @author me */ var source = 'code';
When minifying the code using YUI Compressor, it strips the comments. To include them in the minified file I’ll parse them out the source file and pipe them to the minified file, before appending output from the YUI Compressor.
Cutting to the chase:
sed -n '/^\/\*/p; /^ \*/p' < file.js
A brief description, the first pattern looks for the opening comment:
/^\/\*/
The second pattern matches the rest of the lines.
/^ \*/
The -n
switch suppresses sed
printing output. The /p
modifier duplicates the matched line so combined they mimic grep
behaviour.
Simple eh? It is in hindsight… but I exhaled a few expletives to get here. I guess that’ll teach me for being stubborn, I could have put together a PHP script to preg_match
the comments in two minutes.
comments
No comments for this post.