Tech Tip: View Config Files Without Comments

 in

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.

______________________

Comments

Comment viewing options

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

invert it!

evran's picture

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

Egrep to remove comments and empty lines

Paula R's picture

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

sed trick

Anonymous's picture

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

I like vim syntax

m.roshany's picture

I like vim syntax highlighting in these cases :)

My version of filtering is

rc3's picture

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.

My version of filtering is

rc3's picture

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

I simply do this: $:

Anonymous's picture

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.

Testy are we

Anonymous's picture

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

Use 'cut' instead

Anonymous's picture

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'

Here's mine

Howard Owen's picture

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

"jdp" == "just data please"

I use this command to strip

Edo's picture

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

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

P.S. I made an alias ;)

grep -vE

Anonymous's picture

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

That's not so good if you have some line something like that :

include /bla/bla.conf # include bla

you lost that line if you use that command.

This get very similar

svl's picture

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

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

rc3's picture

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.

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

Seth Rightmer's picture

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

Hi, I also use a lot: $

Anonymous's picture

Hi,

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

In case there are spaces in front of the comments.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState