LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-09-2008, 03:56 PM   #1
callagga
Member
 
Registered: Jan 2008
Posts: 36

Rep: Reputation: 15
Question how can I "cat" or "grep" a file to ignore lines starting with "#" ???


Hi,

How can I "cat" or "grep" a file to ignore lines starting with "#" ???

That is, to list config files that have more comments "# xxxx" than content often. If I just want to see the file without comments and perhaps blank lines.

Cheers
 
Old 07-09-2008, 04:00 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Code:
grep -v '^#' filename
 
Old 07-09-2008, 04:04 PM   #3
callagga
Member
 
Registered: Jan 2008
Posts: 36

Original Poster
Rep: Reputation: 15
arrr, excellent thanks - if I wanted to ignore blank lines as well do you know what it would be then?
 
Old 07-09-2008, 04:09 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
To provide a little more explanation for the above command, the caret '^' represents the start of a line, so you're testing only for # symbols that appear at the beginning of each line. And the '-v' option in grep inverts the selection, so only lines that don't match get displayed.

I suggest working your way through a simple Regular Expression (regex) tutorial (such as this one) if you really want to know how to harness the power of text matching. It's really worth the effort.

Edit: You can pipe the output through grep again to remove the blank lines. There may be more 'proper' ways to do it, but quick and dirty:
Code:
grep -v '^#' filename | grep -v '^$'
The '$' sign matches the end of a line.

Last edited by David the H.; 07-09-2008 at 04:17 PM.
 
Old 07-09-2008, 04:31 PM   #5
callagga
Member
 
Registered: Jan 2008
Posts: 36

Original Poster
Rep: Reputation: 15
thanks - I'd just tried this myself & was pondering a better way to do it
 
Old 07-09-2008, 04:45 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Actually, thinking about it a bit more, a cleaner way would be to use an 'or' operator so you can include both patterns in a single operation. You need to use switch to egrep though.
Code:
egrep -v '(^#|^$)' filename

or alternately

egrep -v '^(#|$)' filename

Last edited by David the H.; 07-09-2008 at 04:50 PM.
 
1 members found this post helpful.
Old 07-09-2008, 04:58 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,053

Rep: Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971
I always use "egrep -v '(^#|^\s*$)' filename" - especially on Debian derived systems.
 
1 members found this post helpful.
Old 08-16-2013, 06:58 AM   #8
kilrogg
LQ Newbie
 
Registered: Aug 2013
Posts: 1

Rep: Reputation: Disabled
Wink

I tend to use egrep -v '(^#|^\s*$|^\s*\t*#)' filename which also excludes comment lines starting with TAB or SPACE and nothing else before the comment (frequent in config files with multi-line comments behind a setting).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 07:27 AM
Stupid question: if [ "$i" == `$(cat ${LOGFILESSHD} | grep "${i}" )` ] ; then frenchn00b Programming 6 05-19-2008 05:16 PM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM


All times are GMT -5. The time now is 12:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration