2 Oneliners For Removing All The Commented Lines In A File

This two oneliners display all the lines in a text file, except the commented lines (lines starting with #). The blank lines are ignored. The output can also be redirected to some text files. See the other oneliner articles here.

$ egrep -v '^#|^$' /path/to/file > /path/to/new_file

$ perl -ne 's/#.*//; print if /\S/' /path/to/file > /path/to/new_file

Be cautious when using this in scripts, these oneliners remove also the shebangs.

Tagged with: , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *




Subscribe to get the latest Linux news and how to guides directly on your e-mail!