Mailinglist Archive: opensuse (2806 mails)

< Previous Next >
Re: [opensuse] Great One-Liner for Parsing Config Files
  • From: "David C. Rankin" <drankinatty@xxxxxxxxxxxxxxxxxx>
  • Date: Wed, 23 Jul 2008 02:41:49 -0500
  • Message-id: <4886E0BD.2010404@xxxxxxxxxxxxxxxxxx>
Mukul Singh wrote:
David C. Rankin wrote:
Rodney Baker wrote:

See? I always 'sed' it was a powerful tool, if only one knew how to use it...;-)

Seriously, thanks Dave for another Really Useful Post. This one definitely gets filed away in my toolbox. :-)


Glad you find it useful. Now all I have to do is wait on the Linux God for my next epiphany.... Like you sed once I figure out sed, they say it is quite useful ;-)

Or you can do :

cat <filename> | egrep -v "^[[:space:]]*$|^#"

I like it, but from what I can tell it doesn't handle the '^:space:#' or '^:space:;' or '^;' situations. I used to rely on egrep alot until working to eliminate '|' pipes in my expressions. Then, it becomes apparent that the flexibility in sed with the ability to author multiple expressions for the same input without piping wins hands down. Using your expression, I would have first eliminated the pipe and cat all together rewriting as:

egrep -v "^[[:space:]]*$|^#" <filename>

No need for two commands with a pipe in the middle where one will do. However, you did help me catch a typo in what I had posted. The expression should have been:

sed -e '/^\s*$/d' -e '/^\s*[#;]/d' <testfile
^^

is the part I messed up.

Now, let's compare. Take the testfile:

02:30 alchemy~/tmp> cat testfile
# comment no space
; php/samba comment no space (2 blank lines follow)


# comment with space
; php/samba comment with space (2 blank line follow with leading spaces)


# comment with tab
; php/samba comment with tab (blank line follows with leading tabs)


my config parameter = hello
parameter2 = again
;# End of Test File

Testing egrep:

02:31 alchemy~/tmp> egrep -v "^[[:space:]]*$|^#" testfile
; php/samba comment no space (2 blank lines follow)
# comment with space
; php/samba comment with space (2 blank line follow with leading spaces)
# comment with tab
; php/samba comment with tab (blank line follows with leading tabs)
my config parameter = hello
parameter2 = again
;# End of Test File

Hmm..

Testing sed:

02:34 alchemy~/tmp> sed -e '/^\s*$/d' -e '/^\s*[#;]/d' <testfile
my config parameter = hello
parameter2 = again

That's what I was expecting...

Don't get me wrong, neither is more right than the other, both work and do what they say they will do. What prompted my post was, being lazy, I found the little sed one-liner to cover a wide range of config files with a minimum of typing. That's the fun of experimenting.

Now to correct my typo........

--
David C. Rankin, J.D., P.E.
Rankin Law Firm, PLLC
510 Ochiltree Street
Nacogdoches, Texas 75961
Telephone: (936) 715-9333
Facsimile: (936) 715-9339
www.rankinlawfirm.com
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >