Tech Tip: View Config Files Without Comments
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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- A Topic for Discussion - Open Source Feature-Richness?
- Dart: a New Web Programming Experience
- Developer Poll
- What's the tweeting protocol?
- May 2013 Issue of Linux Journal: Raspberry Pi
- Reply to comment | Linux Journal
25 min 37 sec ago - Reply to comment | Linux Journal
1 hour 42 min ago - great post
2 hours 17 min ago - Google Docs
2 hours 40 min ago - Reply to comment | Linux Journal
7 hours 28 min ago - Reply to comment | Linux Journal
8 hours 15 min ago - Web Hosting IQ
9 hours 49 min ago - Thanks for taking the time to
11 hours 25 min ago - Linux is good
13 hours 23 min ago - Reply to comment | Linux Journal
13 hours 41 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.



Comments
invert it!
$ grep ^[#] /etc/ntp.conf
Egrep to remove comments and empty lines
egrep -v '^#|^$' /etc/ntp.conf
sed trick
alias cleanconfig='sed -e '\''s/#.*//'\'' -e '\''s/[ ^I]*$//'\'' -e '\''/^$/ d'\'''
I like vim syntax
I like vim syntax highlighting in these cases :)
My version of filtering is
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
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:
I simply do this: $:
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
A bit on the testy side. Get back in the lab, get to work and shut up.
Use 'cut' instead
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
alias jdp='perl -ne "print unless (/^\s*([#!;]|\/\/|$)/);"'
"jdp" == "just data please"
I use this command to strip
I use this command to strip comments from any files I find
grep -vE '(^[[:space:]]*($|(#|!|;|//)))'P.S. I made an alias ;)
grep -vE
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
This get very similar effect, with less type ;)
$ grep -v '#' /etc/ntp.conf
grep -v '#' file might filter out valid lines
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
It won't strip out blank lines like the regex in the article.
Hi, I also use a lot: $
Hi,
I also use a lot:
$ egrep -v "^[[:space:]]*#|^$" /etc/ntp.confIn case there are spaces in front of the comments.