View text files (config files) without comments using grep / egrep

Hi all, here you will learn something very interesting about grep with regexp. I’ve been using grep since many years to trim comments out of config files. We need coments in config files to provide details but sometimes when you want to look through files quickly then comments can get in your way. I’ve found files hundreds of lines long which had fewer than ten active configuration lines, it’s really hard to get an overview of what’s going on when you have to search / walk through hundreds of lines of comments.

  • using following syntax on command line you can ommit comments and just view imp configuration.
    $ grep ^[^#] /etc/ntp.conf

    The regex ^[^#] matches the first character of any line, as long as that character that is not a #. Because blank lines don’t have a first character they’re not matched either, resulting in a nice compact output of just the active configuration lines.

  • i also use this a lot just in case if there are any spaces in front.
    I also use a lot:
    $ egrep -v “^[[:space:]]*#|^$” /etc/ntp.conf

Share this post

Post Comment