Parsing ini files with sed
A while back I found this neat sed pipeline for extracting an ini file section into shell script variables:
INI_FILE=path/to/file.ini
INI_SECTION=TheSection
eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
-e 's/;.*$//' \
-e 's/[[:space:]]*$//' \
-e 's/^[[:space:]]*//' \
-e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
< $INI_FILE \
| sed -n -e "/^\[$INI_SECTION\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`
You now have variables in the scope of your shell script derived from the key/value pairs in that section of the ini script. REALLY handy. Thought it might help someone else if I recorded it here. I got it from this thread on debian-administration.org.NB: the eval is a ksh builtin. I imagine using declare in bash will achieve the same, but I haven't tested it.
NB2: it seems Solaris sed isn't quite up to the task - the above requires gnu sed.
05:07 PM, 08 Nov 2007 by Mark Aufflick Permalink