At 2004-09-09T15:49:57+1200, Andrew Errington wrote:
> I am tinkering with awk to create some XML from a data file. The XML
> generally looks like this:
Perl, Ruby, and Python all have real XML support if you find you need
something heavier.
> <XML header>
> <stuff>
> <things>
> <thing1>
Shouldn't this be:
<thing1 />
<thing2 />
> <thing2>
> .
> .
> .
> </things>
> </stuff>
> I have got awk to produce one instance of the header <XML
> header><stuff><things>, then it trawls the input file and produces
> lots of instances of <thingX>. My question is, how to 'finish off'
> the output file with the footer (/things></stuff> when the input file
> is done?
You might be able to use the BEGIN and END blocks.
BEGIN {
# output header here
}
END {
# output footer here
}
These only run as the script begins and ends, so you won't be able to
use this method if a single script is expected to produce multiple sets
of XML output in a single run.
> Sorry, I am a bit new with awk but I've managed a lot with some help
> from Google, and I do find it a bit cryptic. I suppose I could do it
> in Perl, but I've never tried awk before.
awk is worth learning. The manual for GNU awk is very good (so good, in
fact, that O'Reilly publish it as 'Effective awk Programming'), and 'The
AWK Programming Language' is a classic.
Cheers,
-mjg
--
Matthew Gregan |/
/| [EMAIL PROTECTED]