LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Is there a way to filter out all the comment lines in a configuration file... (https://www.linuxquestions.org/questions/linux-newbie-8/is-there-a-way-to-filter-out-all-the-comment-lines-in-a-configuration-file-390176/)

Akhran 12-07-2005 11:26 AM

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 !

MensaWater 12-07-2005 11:50 AM

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.

GrueMaster 12-07-2005 11:55 AM

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).

sundialsvcs 12-07-2005 12:07 PM

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) :rolleyes:, 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!


All times are GMT -5. The time now is 05:45 AM.