Mohammedz.com

For Linux and Shell scripting.

How can I remove all line starting with hash (#)

2 Comments


To remove all lines starting with hast sign (#) from a file, try this:
sed /^#/d filename

The above command will remove all line starting with # from its output. If you want to write the output to another file, use o/p redirection >
sed /^#/d filename > newfile

But, if you want to over write the file with new o/p, use -i option with sed.
sed -i /^#/d filename

Regards,

Mohammed.

Advertisements

2 thoughts on “How can I remove all line starting with hash (#)

  1. Here is another way to this:

    grep -v ‘^#’ filename

  2. Thank you so much!
    I needed a way to quickly remove all the unnecessary comments from my Apache config file. Hopefully it helps with performance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s