Gplus E-mail
 
 
Home » Uncategorized » Removing Comments from a Configuration File
formats

Removing Comments from a Configuration File

Problem

Sometimes you just gotta remove all blank and commented lines from a configuration file in bash. I didn’t like a lot of the Google hits for this.

Solution

The following code removes all blank lines and all commented lines (including indented ones!) from the passed file. This does NOT deal with inline comments, since you’d have to use some syntax recognition to ensure you didn’t clobber some real bash command. It also leaves any shebang (#!) alone.

cat /path/to/my/file.config | grep -v "^$" | grep -v "^[[:space:]]*#[^\!]"

 
© (c) Ryan E. Guerra 2000-2016
credit