Friday, 21 April 2006

Key-value pair files to xml

This sed script will read in a key-value pairs file, you know, like this:

NAME=Joe & Freds
CLASS=Cafe


into xsl attribute lists, like this:

name="Joe & Freds" class="Cafe"

ready to be stuck in the middle of a tag.

sed -e '
/=/{ # make sure it has an = sign in
h # save current line
s/=.*/=/
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/

x # get back original line
s/^[^=]*=// # loose attribute name
/./{ # anything left?
 s/&/&/
 s/"/"/
 s/</&lt;/
 s/>/&gt;/
 s/^/"/;
 s/$/"/;
 H #append to stored line
 x # get it all back
 s/\n//
 p
}
d
}
d
'


No comments:

Post a Comment