Print only uncommented lines from a text file

A common task that I perform is to print out the lines of a text file (or script) that are not commented and no blank lines.

A few good examples of where this would be useful would be the apache httpd.conf file (which has verbose comments!) and a hosts file where many entries are in use that might not be active.

egrep -v '^$|^\#' /etc/hosts

The above command uses egrep (extended regular expressions) with the -v flag to preclude matches items. The regular expression searches for any lines without content ‘^$’ and anything that starts with a # (pound sign or hash symbol) ‘^\#’.


Comments

Leave a Reply

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