Using grep to removing comments, newlines/whitespace from config files

As configuration files are often full with comments and blank lines reading them inside shell become tedious! Sometimes you just want to quickly recap active configuration option only.

Mostly, comments starts with “#“(hash) or “;” (semicolon).

Below is a command I often use while debugging various configuration files.

For files having comment starting with “;” e.g. php.ini

egrep -v "^([[:space:]]?$|;)" /path/to/file

For files having comment starting with “#” e.g. my.cnf

egrep -v "^([[:space:]]?$|#)" /path/to/file

For lazy bumps… Following can take care of both of above! 

egrep -v "^([[:space:]]?$|;|#)" /path/to/file

You may create a local alias for above command! 😉

Just in case you want to save output in a file… just add " > newfilename" to above commands. Passing original filename may not work (and it will be risky too!)