Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Simple awk help

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-21-2013
Parrakarry Parrakarry is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 9 July 2013, 4:05 PM EDT
Posts: 29
Thanks: 9
Thanked 0 Times in 0 Posts
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).
Sponsored Links
    #2  
Old Unix and Linux 06-21-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 3 July 2015, 9:53 AM EDT
Location: Aachen, Germany
Posts: 6,243
Thanks: 109
Thanked 1,701 Times in 1,593 Posts
Post what you've tried so we can comment on or correct it, respectively.
Sponsored Links
    #3  
Old Unix and Linux 06-21-2013
Parrakarry Parrakarry is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 9 July 2013, 4:05 PM EDT
Posts: 29
Thanks: 9
Thanked 0 Times in 0 Posts

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 Unix and Linux 06-21-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 6 July 2015, 2:02 PM EDT
Location: Saskatchewan
Posts: 20,742
Thanks: 901
Thanked 3,691 Times in 3,450 Posts
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.
Sponsored Links
    #5  
Old Unix and Linux 06-21-2013
Parrakarry Parrakarry is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 9 July 2013, 4:05 PM EDT
Posts: 29
Thanks: 9
Thanked 0 Times in 0 Posts
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>

Sponsored Links
    #6  
Old Unix and Linux 06-21-2013
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 8 December 2014, 6:40 AM EST
Posts: 1,159
Thanks: 20
Thanked 105 Times in 100 Posts

Code:
                if ( $i )
                {
                        print "<entry name=\"KWName"x"\">"$i"</entry>"
                        x=x+1;
                }

The Following User Says Thank You to panyam For This Useful Post:
Parrakarry (06-21-2013)
Sponsored Links
    #7  
Old Unix and Linux 06-21-2013
Parrakarry Parrakarry is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 9 July 2013, 4:05 PM EDT
Posts: 29
Thanks: 9
Thanked 0 Times in 0 Posts
Dear god. I am not a clever man. This is why I ask you guys instead of my coworkers :P
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Simple awk use shekhar2010us Shell Programming and Scripting 6 01-31-2013 02:42 AM
Need help with simple comparison in AWK klusps Shell Programming and Scripting 7 05-01-2012 06:19 AM
help with simple awk script fwellers Shell Programming and Scripting 4 06-11-2010 08:35 AM
simple awk help linuxdude Shell Programming and Scripting 2 08-27-2008 07:39 AM
simple awk talashil Shell Programming and Scripting 7 06-10-2008 02:31 PM



All times are GMT -4. The time now is 07:04 AM.