Hello again everyone,
yes, I'm back again for more help! So I'm attempting to read two separate files and generate some XML code from that. My current code is:
BEGIN {
print "<?xml version=\"1.0\" encoding=\"utf-8\">"
print "<Export>"
}
{
x=1;
print "<section name=\"Query" NR "\">"
print "<entry name=\"DocumentType\">" $1 "</entry>"
for ( i = 2; i <= NF; i++ )
{
if ( $i )
{
print "<entry name=\"KWName"x"\">"$i"</entry>"
x=x+1;
}
}
print "</section>"
}
END {
print "</Export>"
}
It reads one file, and generates exactly what I need from that one (looks like):
<?xml version="1.0" encoding="utf-8">
<Export>
<section name="Query1">
<entry name="DocumentType">Document Type</entry>
<entry name="KWName1">Project Number</entry>
<entry name="KWName2">Org ID</entry>
<entry name="KWName3">Invoice Number</entry>
</section>
<section name="Query2">
<entry name="DocumentType">Invoices</entry>
<entry name="KWName1">Project Number</entry>
<entry name="KWName2">Org ID</entry>
<entry name="KWName3">Invoice Number</entry>
</section>
<section name="Query3">
<entry name="DocumentType">Requisitions</entry>
<entry name="KWName1">Invoice Number</entry>
</section>
<section name="Query4">
<entry name="DocumentType">Proposals</entry>
<entry name="KWName1">Project Number</entry>
<entry name="KWName2">Org ID</entry>
</section>
</Export>
when run as
awk -F, -f test.awk input.csv
HOWEVER, what I want to be able to do is something along the lines of:
{
if ( $i )
{
print "<entry name=\"KWName"x"\">"$i"</entry>"
LOOP THROUGH ANOTHER FILE AND PRINT ALL LINES 1 BY 1
x=x+1;
}
with the "pseudocode" in caps. Ultimately, I'm going to want my final product to look something along the lines of
<?xml version="1.0" encoding="utf-8">
<Export>
<section name="Query1">
<entry name="DocumentType">Document Type</entry>
<entry name="KWName1">Project Number</entry>
<entry name="KWValue1">12345</entry>
<entry name="KWValue2">12346</entry>
<entry name="KWName2">Org ID</entry>
<entry name="KWValue1">12345</entry>
<entry name="KWValue2">12346</entry>
...
I have tried nesting awk statements, I've tried
for line in testfile do echo "$line" done
, i've even tried an if statement, and it always seems to throw an exception in awk. I'm using KSH with awk. Can I not use shell commands inside awk? What am I missing? Thanks in advance for all your help!