Tech Tip: View Config Files Without Comments

August 4th, 2009 by David Berner in

Your rating: None Average: 4.5 (31 votes)

I've been using this grep invocation for years to trim comments out of config files. Comments are great but can get in your way if you just want to see the currently running configuration. 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 wade through hundreds of lines of comments.

$ 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.

__________________________


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
evran's picture

invert it!

On September 26th, 2009 evran (not verified) says:

$ grep ^[#] /etc/ntp.conf

Paula R's picture

Egrep to remove comments and empty lines

On August 15th, 2009 Paula R (not verified) says:

egrep -v '^#|^$' /etc/ntp.conf

Anonymous's picture

sed trick

On August 13th, 2009 Anonymous (not verified) says:

alias cleanconfig='sed -e '\''s/#.*//'\'' -e '\''s/[ ^I]*$//'\'' -e '\''/^$/ d'\'''

m.roshany's picture

I like vim syntax

On August 12th, 2009 m.roshany (not verified) says:

I like vim syntax highlighting in these cases :)

rc3's picture

My version of filtering is

On August 9th, 2009 rc3 (not verified) says:

My version of filtering is very similar to the last comment before mine:
egrep -v "^[[:cntrl:] ]*[#;] file
This also filters out comment lines starting with tab(s), followed by # or ; (openvpn's configuration file is a good example that uses both # and ; as the comment characters.)

To make filtering easier, I add the following function into my ~/.bashrc

cf() { [ $# -lt 1 ] && echo "Usage: $0 file" && return; egrep -v "^[[:cntrl:] ]*[#;] $1; }

If I want to view a config file without comment and empty lines, I simply do a

cf config_file

Cheers.

rc3's picture

My version of filtering is

On August 10th, 2009 rc3 (not verified) says:

Sorry, dropped the part that filters empty lines, here's the corrected one:
egrep -v "^[[:cntrl:] ]*[#;]|^$"

And I found it to be more convenient to define the above command in alias, i.e., I add this line to ~/.bashrc

alias cf='egrep -v "^[[:cntrl:] ]*[#;]|^$"'

Now I can use it in either way of the followings:

cat config_file|cf

cf config_file

Anonymous's picture

I simply do this: $:

On August 6th, 2009 Anonymous (not verified) says:

I simply do this:

$: "editor" /etc/file_to_view

where "editor" is your favorite editor...then I dynamically filter out the comments using my brain.

come on, this tip is sooo basic - are we going to permute all the possible little CLI tricks one can do and then
compete with one another over who's script is better? - geez.

Anonymous's picture

Testy are we

On August 10th, 2009 Anonymous (not verified) says:

A bit on the testy side. Get back in the lab, get to work and shut up.

Anonymous's picture

Use 'cut' instead

On August 6th, 2009 Anonymous (not verified) says:

Better use cut to clear comments from your files. This way you can clear away comments after regular text too. If you want to get rid of extra newlines, you can add a tr.

cut -f1 -d"#" FILENAME | tr -s '\n'

Howard Owen's picture

Here's mine

On August 6th, 2009 Howard Owen (not verified) says:

alias jdp='perl -ne "print unless (/^\s*([#!;]|\/\/|$)/);"'

"jdp" == "just data please"

Edo's picture

I use this command to strip

On August 6th, 2009 Edo (not verified) says:

I use this command to strip comments from any files I find

grep -vE '(^[[:space:]]*($|(#|!|;|//)))'

P.S. I made an alias ;)

svl's picture

This get very similar

On August 5th, 2009 svl (not verified) says:

This get very similar effect, with less type ;)
$ grep -v '#' /etc/ntp.conf

rc3's picture

grep -v '#' file might filter out valid lines

On August 9th, 2009 rc3 (not verified) says:

Be careful when you use
grep -v '#' file
as it'll filter lines containing #'s, regardless # is the beginning character of a line or not. In other words, lines like this will be filtered as well:
var1=some_value # define var1
Obviously you don't want that line to be filtered.

Seth Rightmer's picture

I've used that one, too, but it's not as good

On August 5th, 2009 Seth Rightmer (not verified) says:

It won't strip out blank lines like the regex in the article.

Anonymous's picture

Hi, I also use a lot: $

On August 4th, 2009 Anonymous (not verified) says:

Hi,

I also use a lot:
$ egrep -v "^[[:space:]]*#|^$" /etc/ntp.conf

In case there are spaces in front of the comments.

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.


Read this issue