LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-16-2012, 04:14 AM   #1
ekcr
LQ Newbie
 
Registered: Nov 2011
Posts: 9

Rep: Reputation: Disabled
GREP


Hi Linux Gurus
Could someone tell me how I can grep all uncommented lines in a file on linux?

Thank you in advance.
 
Old 02-16-2012, 04:25 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 192Reputation: 192
What have you done so far?

You should look at the "-v" option of grep, and the "^" regex pattern.
 
Old 02-16-2012, 04:29 AM   #3
ekcr
LQ Newbie
 
Registered: Nov 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
I have used 'egrep -v '^#' file.conf' but the problem is that it has so many blank lines/spaces. Any suggstion?
 
Old 02-16-2012, 04:43 AM   #4
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 192Reputation: 192
So, what is your problem?
What are you REALLY trying to do?

Are you saying that you want to remove blank lines as well?

Code:
grep -v ^# filename | sed '/^$/d'
Will show only uncommented lines, with no blank lines.
 
Old 02-16-2012, 04:44 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
This will exclude comment lines with leading spaces, TABs and so on (if any) and empty lines with spaces (if any). It will preserve lines of code with trailing comments:
Code:
grep -E -v '^[[:space:]]*#|^[[:space:]]*$' file
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep has no effect - does not grep anything in this for loopa LinuxChiq Linux - Newbie 2 12-01-2011 09:03 PM
[SOLVED] Grep -p for Linux, Trying to grep a paragraph. ohijames Linux - Newbie 5 07-22-2010 02:09 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:08 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration