Mailinglist Archive: opensuse (2806 mails)

< Previous Next >
[opensuse] Great One-Liner for Parsing Config Files
  • From: "David C. Rankin" <drankinatty@xxxxxxxxxxxxxxxxxx>
  • Date: Mon, 21 Jul 2008 15:31:16 -0500
  • Message-id: <4884F214.5090708@xxxxxxxxxxxxxxxxxx>
Listmates,

I'm always looking for easier ways to do things and pass along worthwhile epiphanies as they occur. For the gurus, this is child's play, but I can recall parsing config files as a newbie and I always wanted to get rid of the comments and just look at the active config parameters. At that time, grep, sed, awk and the like just looked like word fragments from a foreign language.

Continuing in that tradition, here is another post concerning making it easy to parse config files to get to the "beef". From the command line, a great one-liner that will do this is:

Command Line: sed -e '/^[/s]*$/d' -e '/^\s*[#;]/d' <filename

EXAMPLE: sed -e '/^[/s]*$/d' -e '/^\s*[#;]/d' </etc/apache2/httpd.conf

The two sed expressions handle quite a number of situation. The first simply removes blank lines from the output. The second (after the 2nd -e) removes all lines that begin with '#' or ';' regardless whether there is whitespace before the '#' or ';' character. If you have a config file that uses other than '#' or ';' to start the comment you can simply add the character to the character class in between the '[xy z]' brackets.

Nobody wants to type "sed -e '/^[/s]*$/d' -e '/^\s*[#;]/d' <filename" each time you want to parse a file, so let's make it real easy. A quick script:

#!/bin/bash

if [[ -z "$1" ]]; then
echo -e "\n\n\tUsage: nc <filename>"
echo -e "\n\n\tWill parse the text file and remove all blank lines and all lines beginning \
\n\twith an '#' or ';'. It is useful for looking at configuration files.\n"
exit 1
fi

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

exit 0

Check that "nc" is not already used as a name for an executable by simply typing "nc" at the command line -- the error command not found means "nc" is OK to use. Now save the file as "nc" (short for no-comment) in either "~/bin" (to have it available to only you) or save it in "/usr/local/bin" (to make it available to everyone).

Now when you want to parse a config file, it is as easy as:

nc filename

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