Nested awk Statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested awk Statements
# 1  
Old 06-24-2013
Nested awk Statements

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:

Code:
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):
Code:
<?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

Code:
awk -F, -f test.awk input.csv

HOWEVER, what I want to be able to do is something along the lines of:

Code:
	{
		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

Code:
<?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
Code:
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!
# 2  
Old 06-24-2013
Specify all the input files to awk one by one on the command line...
Code:
ls -1 <input_file_filter> | while read file
do
    awk -F, test.awk $file
done

# 3  
Old 06-24-2013
I'm not sure I see how that would result in the output I desire. I suppose I could pipe all the fields within the awk statements into two seperate arrays, and then generate the xml after I've done that, or something similar. Will have to do some testing. Sorry, I'm very new at this...
# 4  
Old 06-24-2013
Try enumerating your input files to awk:
Code:
awk -F, -f test.awk input1.csv input2.csv input3.csv

or use some well formed pattern (e.g. input[1-3].csv).
# 5  
Old 06-24-2013
Well then your requirements should be as clear as possible as I cant make much out of what you have now...so post what you want in as simple words as possible along with a sample of the input(s) and output(s)...
# 6  
Old 06-24-2013
tl;dr: I should wait until I hear more from my boss before asking confusing questions.

I have a list of hundreds of different document types, and the various keywords associated with them in our document storage system. Unfortunately, I have not received the required values for the keywords yet, and I know that the program we are feeding the XML into will slow waaaay down when you try to do too much at once. The idea is, I will generate XML in the format
Code:
<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="KWValueN">Nth Project Number</entry>
</section>
<section name="Query2">
<entry name="DocumentType">Invoices</entry>
<entry name="KWName1">Project Number</entry>
<entry name="KWValue1">12345</entry>
<entry name="KWValue2">12346</entry>
..
<entry name="KWValueN">Nth Project Number</entry>
</section>

then feed it into another program which will spit out all documents of that type with the listed keywords matching the listed values.

I actually think RudiC's suggestion may well solve the problem though, unless my boss requires multiple keywords in each XML file. My initial question was regarding being able to sort of switch back and forth between files: read a KWName, then move to the other input file, where each row is filled with numbers for a specific KWName, enumerating the KWValues for each one, then switch back and print the next KWName. However, writing this all out has made me realize that it's possible I will only have to generate a file for each KWName, in which case it should be readily doable using multiple inputs in one awk statement. I didn't even know that was a thing. Also, he's way smarter than me and can probably explain how to do this, I was just impatient and wanted to get a head start (solving the problem "by myself" is a good way to look good!) Sorry for wasting your time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested if statements with word/number extracts

Hi everyone! I'm having difficulties setting up a complex script with nested if statements while doing some word extracts, any help is appreciated. Scenario: 1- Check if the file.txt has the word BINGO 2- If so then get the available number (any number) in the line that contains the word... (8 Replies)
Discussion started by: demmel
8 Replies

2. Shell Programming and Scripting

awk with many if statements

Hi What is the right structure to use awk with multiple If statements The following code doesn't work # awk ' { A = $1 } END { for ( i = 1; i <= c; i++ ) { if ( A == 236 && A ==199... (7 Replies)
Discussion started by: khaled79
7 Replies

3. Shell Programming and Scripting

awk problem - combining awk statements

i have a datafile that has several lines that look like this: 2,dataflow,Sun Mar 17 16:50:01 2013,1363539001,2990,excelsheet,660,mortar,660,4 using the following command: awk -F, '{$3=strftime("%a %b %d %T %Y,%s",$3)}1' OFS=, $DATAFILE | egrep -v "\-OLDISSUES," | ${AWK} "/${MONTH} ${DAY}... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. UNIX for Dummies Questions & Answers

Combine two awk statements into one

Hi, I have the following two awk statements which I'd like to consolidate into one by piping the output from the first into the second awk statement (rather than having to write kat.txt out to a file and then reading back in). awk 'BEGIN {FS=OFS=" "} {printf("%s ", $2);for (x=7; x<=10;... (3 Replies)
Discussion started by: kasan0
3 Replies

5. Shell Programming and Scripting

Nested looping statements

I cannot get the code below to work correctly. The IF statement works just fine, but not the looping. The inner loop tries to find files for a given vendor; if found, I need to sleep giving another process time to move the files. Once the given vendor's files are gone, then I want to move on to the... (1 Reply)
Discussion started by: dgreene
1 Replies

6. Shell Programming and Scripting

syntax question in regards to nested awk statements

Hello all, I am writing up an input file and I was hoping I could get some guidance as to how to best consolidate these 2 awk statements for 1 while loop. Here's my input file # cat databases.lst #NOTE: These entries are delimited by tabs "\t" #oracleSID name/pass # db11 ... (2 Replies)
Discussion started by: Keepcase
2 Replies

7. UNIX for Dummies Questions & Answers

AWK w/ if statements failing.

I'm converting some code from ksh on my macbook (Version M 1993-12-28 s+) to an older solaris machine with ksh 88. I can't seem to figure out this line, it worked on the new shell version. set -A combo -- $(for x in ${ImageIDs}; do nawk -v s=$x 'if($2 == s) getline ; getline if ($1 ==... (2 Replies)
Discussion started by: nerdcurious
2 Replies

8. Shell Programming and Scripting

Nested if statements with && and ||?

Hello, I'm a shell scripting noob and new to this forum as well. My question is can nested if statements be done with && and || instead and if it can be done can someone provide an example pls. Thanks in advance for the help (1 Reply)
Discussion started by: zomgshellscript
1 Replies

9. Shell Programming and Scripting

Help a newbie please with awk if else statements

Hi, Despite reading the Conditional Statements chapter in the O'Reilly Sed & Awk book several times and looking at numerous examples, I cannot for the life of me get any kind of if ... else statement to work in my awk scripts! My scripts work perfectly (as they are written at least) and do what... (4 Replies)
Discussion started by: jonathanm
4 Replies

10. Shell Programming and Scripting

awk compound statements

how can i use two or multiple statements in the if part of an awk code for example i want to check two flag if they are true i will write some print operations and increase the counter. here is the c version of the code that i want to write: counter=0; if (flag1==1 && flag2==0) {... (7 Replies)
Discussion started by: gfhgfnhhn
7 Replies
Login or Register to Ask a Question