LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-07-2005, 10:26 AM   #1
Akhran
Member
 
Registered: Aug 2005
Distribution: Debian 'lenny'
Posts: 208

Rep: Reputation: 30
Is there a way to filter out all the comment lines in a configuration file...


... and redirect the filtered output into another file? So, basically what is saved in the new file is only the configuration lines without all the comment lines (ie. lines starting with #).

Thanks !
 
Old 12-07-2005, 10:50 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
grep -v ^# inpufile > output file

This will get eliminae all lines where # is the first character of the line. Most (but not all) comments start with # in the first position of the line. You wouldn't want to do "grep -v #" as there may be a comment embedded on an instruction line.
 
Old 12-07-2005, 10:55 AM   #3
GrueMaster
Member
 
Registered: Aug 2005
Location: Oregon
Distribution: Kubuntu.
Posts: 848

Rep: Reputation: 30
Try fgrep -v # <oldfile> > <newfile>. This will strip every line that contains #. If your config files have comments after config settings (i.e. Test=1 # Enable test), then you should use a different alternative, like piping through awk as follows:

cat <old file> |awk 'BEGIN{FS="#"}{print $1}' ><newfile>

That will leave you with a clean file, although there will be quite a lot of white space (which I'm sure there is another way to clean up, just don't remember off hand).
 
Old 12-07-2005, 11:07 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
There are two Linux commands, and one related concept, that are extremely powerful and that you should take the time to master because they will save you hours of work...

(Concept) Regular Expressions: A very lovely, fanciful name for "a pattern for matching strings," and also for slicing pieces out of those strings. (See: man 7 regex.) The cryptic "^#" is a pattern which matches "the character '#' occurring at the beginning of a line." The even-more-cryptic "^[:space:]*#" is a pattern which matches "the character '#', optionally preceded by zero or more blanks, tabs, or other "white space" characters, occurring at the beginning of a line."

(Command) grep, which filters out the lines of a file which match (or which do not match, a particular regular-expression. (See: info grep.)

(Command) awk, which is an even more sophisticated program for parsing (splitting things out of...) text files. An AWK "program" consists of a list of regular-expressions or conditions, and a corresponding series of statements which tell AWK what to do when each expression or condition is matched. (See: info awk.)

These commands and their descriptions were written by rocket scientists (literally) , so they're rather obscure to say the least, but as usual, the essential idea behind each tool or concept is a simple one. Start with a firm grip on what that simple idea is, read slowly and multiple times, and the various complexities will snap into place around it. The time spent is well worth it. These are power tools!
 
  


Reply



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
how to filter output from passwd file mussussu Linux - General 2 11-28-2005 04:17 PM
bash: read lines from a configuration script ldp Programming 2 09-23-2005 11:58 AM
php filter file name question feetyouwell Programming 1 01-31-2005 12:48 PM
how to open file in filter program? jayashrik Programming 1 01-26-2005 01:48 AM
How to filter lines from a text file Rascale Linux - Software 1 10-28-2003 12:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:08 PM.

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