Solved

Command to Hide Comments

Posted on 2002-07-09
13
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +2
13 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 7142195
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
ID: 7143371
I'll second that.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7143970
# only one process needed:
  awk '($1 ~ /^#/){next}(NF==0){next}{print}' file
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:davidpm
ID: 7147748
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
ID: 7148539
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
ID: 7148647
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
 
LVL 3

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

by:chris_calabrese
ID: 7189200
What the heck...

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

Expert Comment

by:ahoffmann
ID: 7190003
contest closed?
LOL
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7313884
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
ID: 7314188
I think it was the easiest to read. Regular expressions can get complicated.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
can i read my emails on lamp ftp 4 71
Krita v3 Linux Mint/Ubuntu 16.04 9 76
How to check if the Cron job is running or not ? 12 87
Linux recover lost file deleted by mv 1 32
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

710 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