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.

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

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

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

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