Simple awk help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple awk help
# 1  
Old 06-21-2013
Simple awk help

Hey all,

so I'm using AWK in a project at work, to generate xml from csv. So far things are going relatively smoothly, but I have one thing I can't figure out.

For every field in each row, I must generate
Code:
<entry name=KWNamex>Field</entry>

Then I will need to pull data from a second file and generate
Code:
<entry name=KWValuex>Field</entry>

Disregarding the multi file input, which I haven't even began working on yet, how can I increment the value of "x" for each successive field in the record? Every time I try to use a variable, it pulls the value for that numbered field. For example, if I have PROJECT NUMBER in the first field, PROJECT NAME in the second, and USERID in the third, I want an output of:

Code:
<entry name=KWName1>PROJECT NUMBER</entry>
<entry name=KWName2>PROJECT NAME</entry>
<entry name=KWName3>USERID</entry>

This is probably the dumbest question ever, but I can't seem to use any variables without them providing the data inside that numbered field. Also, some rows won't have certain fields, so hardcoding the KW number for each column will not work (and would be a very sloppy solution anyway).
# 2  
Old 06-21-2013
Post what you've tried so we can comment on or correct it, respectively.
# 3  
Old 06-21-2013
Code:
BEGIN {
print "<?xml version=\"1.0\" encoding=\"utf-8\">"
print "<Export>"
}
{
	print "<section name=\"Query" NR "\">"
	print "<entry name=\"DocumentType\">" $1 "</entry>"
	for ( i = 2; i <= NF; i++ )
	{
		if ( $i )
                	print "<entry name=\"KWName1\">"$i"</entry>"
	}
	print "</section>"
}
END {
print "</Export>"
}

This is stored in
Code:
test.awk

, then I run:

Code:
awk -F, -f test.awk testfile.txt

testfile.txt is:

Code:
Document Type,Project Number,Org ID,Invoice Number
Invoices,Project Number,Org ID,Invoice Number
Requisitions,,,Invoice Number
Proposals,Project Number,Org ID,

And the output I get is:

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="KWName1">Org ID</entry>
<entry name="KWName1">Invoice Number</entry>
</section>
<section name="Query2">
<entry name="DocumentType">Invoices</entry>
<entry name="KWName1">Project Number</entry>
<entry name="KWName1">Org ID</entry>
<entry name="KWName1">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="KWName1">Org ID</entry>
</section>
</Export>

Every time I set a variable to use as a counter for KWName, I end up calling the field that the counter relates to. So if I tried
Code:
x=1

and then increment x by 1 for every time I generate a KWName, I end up with something like
Code:
<entry name="KWNameInvoices">Invoices</entry>

I understand this is due to awk's use of variables to relate to fields. My question is whether I can use a variable as just a plain old variable within this awk statement, or if there's some other technique I can use to get the output I want. Thanks for the help!
# 4  
Old 06-21-2013
You've given us the output you don't want, not the output you do want, so I'm still kind of puzzled as to what you actually want.
# 5  
Old 06-21-2013
The output I want is:

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>

# 6  
Old 06-21-2013
Code:
                if ( $i )
                {
                        print "<entry name=\"KWName"x"\">"$i"</entry>"
                        x=x+1;
                }

This User Gave Thanks to panyam For This Post:
# 7  
Old 06-21-2013
Dear god. I am not a clever man. This is why I ask you guys instead of my coworkers :P
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple awk

Hi, this must be a simple but this is my first interaction with shell and awk. following is a log file needed to parse (2 lines separated by a line break for clarity): 2013-07-27 13:32:09,043 - ERROR - PerformanceUtility - Thread-14 - Performance - 9b348407-4f57-4983-a057-a55669821f68 |... (12 Replies)
Discussion started by: liv2luv
12 Replies

2. Shell Programming and Scripting

Simple awk use

Hi, I have a file such that: 40454,31,48,4,1304.967741935484,1 31708,25,48,4,1268.32,1 20900,64501,671,788,0.3240259840932699,0 20137,51358,834,743,0.3920908135051988,0 I want to replace the 6th column by "ones" if it is 1, and with "zeros" if it is 0. Thanks. (6 Replies)
Discussion started by: shekhar2010us
6 Replies

3. Shell Programming and Scripting

Need help with simple comparison in AWK

Hi, I'm new to AWK and I'm having problems comparing a field to a string variable. /ARTIST/ {x = $2} $1 ~ x {print $0}My code tries to find a record with the string "ARTIST". Once it finds it, it stores the second field of the record into a variable. I don't know what the problem is for the... (7 Replies)
Discussion started by: klusps
7 Replies

4. Shell Programming and Scripting

another simple awk problem

Hello; I need to print two previous lines after searching for a reg exp: awk '/haywood/' should produce the following =================== p9J46THe020804 89922 Tue Oct 18 21:06 MAILER-DAEMON (host map: lookup (haywood.com): deferred) ... (1 Reply)
Discussion started by: delphys
1 Replies

5. Shell Programming and Scripting

Simple awk problem II

Hello; Trying to figure out how to keep just the contents between the two search lines: awk '/regexp_1/ ,/regexp_2/' I do not want lines containing regexp_1 and regexp_2 in the output. Thank you for any ideas Video tutorial on how to use code tags in The UNIX and Linux Forums. (5 Replies)
Discussion started by: delphys
5 Replies

6. UNIX for Dummies Questions & Answers

Please explain this simple AWK example

awk '!_++' Most importantly, I want to know what the underscore does "!_" But ideally, please breakdown the whole thing. It is supposed to remove duplicate lines when found in a file. (1 Reply)
Discussion started by: glev2005
1 Replies

7. Shell Programming and Scripting

help with simple awk script

Hi, I just don't understand awk. I think I'm close here but can someone give me a hand to finish this little parsing routine ? the input file is formatted like this: District 2502110 Gsub 2384889 Gsub 1428180 District 2502220 Gsub 1466390 Gsub 1466389 Gsub 1466388 Gsub 1466386 Gsub... (4 Replies)
Discussion started by: fwellers
4 Replies

8. Shell Programming and Scripting

simple awk help

Whats the syntax to find all lines that matches a text and print out specific fields after the match? ex: 1: some random text 2: Full name: John E. Smith 3: some random text 4: Full name: Mary J. Lue 5: some random text So I'd like to print out First names or last names or everything... (2 Replies)
Discussion started by: linuxdude
2 Replies

9. Shell Programming and Scripting

simple awk

when I execute this awk stmt .. awk "/log_directory/ { print $5}" /opt/dba/oraadmin/tools/tmp_purge_op.log it's returning the whole line as .. IRMD118_LISTENER1 parameter "log_directory" set to /opt/oracle/10.2/network/log/ my expected output is : /opt/oracle/10.2/network/log what... (7 Replies)
Discussion started by: talashil
7 Replies

10. Shell Programming and Scripting

Help with a simple script using awk

I need a hand with this simple script, in Unix i have a variable called portal: $ echo $portal chu0 when i use awk this variable is not recognized. How can i make awk recognize and print the value of this variable, since the output is not shown i.e. awk ' BEGIN {printf("%4s\t%14s\n",... (3 Replies)
Discussion started by: alexcol
3 Replies
Login or Register to Ask a Question