Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Remove comments from a file (See related posts)

Remove all comment lines from a file.

sed -e '/^#/d' $1 | more

or to output it to a new file
sed -e '/^#/d' $1 > $1.nocomments

Comments on this post

sundaryourfriend posts on Jun 21, 2007 at 13:58
Changing that to
sed -e '/^#[^!]/d' chngcase.sh | less
would prevent the shebang line from getting removed.
ghettonet posts on Nov 10, 2007 at 01:47
Thanks for posting that - theres a problem though - it wont remove comments that are indented, i.e. there are spaces between the comment symbol # and the left hand margin. it also doesnt work if the comment is on the same line behind a command.
ghettonet posts on Nov 11, 2007 at 03:15
Ok - i fixed it with the help of the super cool guys in the #bash irc channel, so i'll post it here for anyone who needs it.

sed '1p; /^[[:blank:]]*#/d; s/[[:blank:]][[:blank:]]*#.*//'

- this will leave the shebang line alone, and will delete all comments except for the inline ones (too risky - it could delete any '#' symbols used in the script itself)

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts