LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 07-11-2009, 07:25 AM   #1
aarav2306
Member
 
Registered: Jan 2009
Posts: 55

Rep: Reputation: 15
Extract uncommented lines from a file


Hi
I want to see only the uncommented lines of a conf file.
Is there a simple shell command for this.
Am quite new to Linux and did search the forums for answers to what might be a simple question.
Maybe it is far too simple to be queried in this forum
 
Old 07-11-2009, 07:33 AM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Many conf files use the # character to designate a comment. Therefore you can use the grep utility to filter out the lines that you don't want to see.
Code:
grep -v \# <filename>
The usual behavior of grep is to display the lines in a file containing the search character(s). Here the -v reverses the behavior of grep. The \ character tells the Bash shell to pass the next character to grep rather than interpret it as a regular expression.

More information can be found in the man pages. (Man is short for manual.)
Code:
man grep
Or the grep utility will provide similar information.
Code:
grep --help

Last edited by stress_junkie; 07-11-2009 at 07:37 AM.
 
Old 07-11-2009, 07:34 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Code:
grep -v "^#" filename
Translation:
find all lines beginning with "#"
return everything except those lines (-v option)

Go to http://tldp.org and get a copy of Bash Guide for Beginners
 
Old 07-11-2009, 07:39 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,792

Rep: Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923
Simple questions are rarely simple.
What about lines that don't actually start with # ?.
What about multi-line comments ?.
What about embedded (multi-line) comments ?.

Properly define the problem.
 
Old 07-11-2009, 07:44 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
You may want to mask also blank lines. In that case you can pipe the output of the suggested commands to grep -v ^$ or use the following sed command:
Code:
sed '/^$\|^#/d' filename
 
Old 07-11-2009, 07:49 AM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 10,882
Blog Entries: 1

Rep: Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307
Hi,
Quote:
Originally Posted by aarav2306 View Post
Hi
I want to see only the uncommented lines of a conf file.
Is there a simple shell command for this.
Am quite new to Linux and did search the forums for answers to what might be a simple question.
Maybe it is far too simple to be queried in this forum
What have you attempted on the file for the task?

'Advanced Bash-Scripting Guide' would be a great tutorial to read to gain understanding.

The above link and others available from 'Slackware-Links'. More than just SlackwareŽ links!
 
Old 07-11-2009, 11:27 AM   #7
aarav2306
Member
 
Registered: Jan 2009
Posts: 55

Original Poster
Rep: Reputation: 15
Thanks
I used the sed command as I wanted to mask the blank lines as well, hadn't realised I would need to mask that as well
aarav
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extract matching lines from File2 based on File1. grep+while? pwd_pwd_omg_pwd Linux - Newbie 5 04-07-2009 01:48 PM
extract contents between two empty lines vikas027 Programming 6 12-19-2008 09:52 AM
Extract lines containing some strings without affectting sequential order cgcamal Programming 7 11-06-2008 11:57 PM
Extract lines NOT on a block of text from a file Renan_S2 Programming 3 10-05-2008 04:14 PM
how to extract certain lines from a log file Avatar Linux - Newbie 3 02-11-2005 09:51 AM


All times are GMT -5. The time now is 11:43 AM.

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