Begin main content

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 | Short Link | Comments (0)

XML

Blog Categories

software (27)
..cocoa (13)
  ..heads up 'tunes (5)
..ruby (4)
..lisp (1)
..perl (3)
..openacs (1)
mac (18)
embedded (2)
..microprocessor (2)
  ..avr (1)
electronics (3)
design (1)
photography (24)
..black and white (6)
..A day in Sydney (18)
..The Daily Shoot (5)
food (2)

Notifications

Icon of Envelope Request notifications

Syndication Feed

XML

Recent Comments

  1. Unregistered Visitor: To replace current categories or remove them...
  2. Unregistered Visitor: Base 256
  3. Unregistered Visitor: ...
  4. Unregistered Visitor: www.pipl.com
  5. Unregistered Visitor: This reminds me of the infinite monkeys
  6. Unregistered Visitor: Feedback
  7. Unregistered Visitor: Diito... Snow Leopard no worky
  8. Unregistered Visitor: WFM
  9. Unregistered Visitor: Pie
  10. Unregistered Visitor: Helpful