Solved

Command to Hide Comments

Posted on 2002-07-09
13
197 Views
Last Modified: 2013-12-15
I am often presented with config files modified by someone else and would like to make sure they are correct.
The problem with most config files is that there are only a few lines of active code and often hundreds of lines of comments.

Is there a simple bash command that will cat a file and skip all blank lines and all lines that start with #?

One of my techs wrote a perl script but that seems overkill.

0
Comment
Question by:davidpm
  • 6
  • 2
  • 2
  • +2
13 Comments
 
LVL 38

Expert Comment

by:yuzh
Comment Utility
use can use the following command line to do the job:

assume your file name is: yourfile.txt

cat yourfile.txt | grep -v "^#" | grep -v "^$" | more

you can redirect to "less" or another file.
0
 
LVL 14

Expert Comment

by:chris_calabrese
Comment Utility
I'll second that.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
# only one process needed:
  awk '($1 ~ /^#/){next}(NF==0){next}{print}' file
0
 

Author Comment

by:davidpm
Comment Utility
Thanks ahoffman but I'm ready for another language just yet.

The following works:
cat test.conf | grep -v "^#" | grep -v "^[[:space:]]*$"

The last grep means match if the line only has zero or more whitespace characters in it. This should match space and tab etc.

That last part is a little hard to remember. Is there a trimmer way of doing the same thing?


0
 
LVL 38

Accepted Solution

by:
yuzh earned 50 total points
Comment Utility
You can define a function in your .profile

eg:

readconf ()
{
cat $1 | | grep -v "^#" | grep -v "^[[:space:]]*$"

}


to use it:

readconf test.conf
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
FYI,
my suggestion works with any flaviour of awk on any UNIX,
"[[:space]]" is GNU-grep (and derivates) only (well, the default on Linux anyway).
And if performance or resources are in question: only one process ;-)
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 3

Expert Comment

by:DVB
Comment Utility
egrep -v "(^#|^$)" /path/to/file
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
DVB: s/"/'/g
 and it still does not remove all comment lines
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
egrep -v '([ \t]*#|^[ \t]*$)' file
0
 
LVL 14

Expert Comment

by:chris_calabrese
Comment Utility
What the heck...

egrep -v '^[ \t]*(#|$)' file
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
contest closed?
LOL
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
wow, the longest (characters and cpu time) suggestion got the grade ;-)

davidpm, could you please explain your decision?

(yuzh, no offence)
0
 
LVL 3

Expert Comment

by:DVB
Comment Utility
I think it was the easiest to read. Regular expressions can get complicated.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now