CSV to XML


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CSV to XML
# 8  
Old 09-10-2005
AWK Script Execution!

vgersh99,
Since you gave the input as a text file, the output that you got was correct. Instead of using the text file, I used the csv file as under :

pj.csv file
---------
"Store Number", "Quarter 1 Sales", "Quarter 2 Sales", "Quarter 3 Sales", "Quarter 4 Sales"

"1", "35%", "29%", "59%", "42%"
"2", "61%", "37%", "34%", "86%"
"3", "68%", "47%", "62%", "18%"
"4", "78%", "83%", "91%", "78%"
"5", "53%", "44%", "67%", "56%"

pj.awk file
----------
BEGIN {OFS = "\n"}
NR == 1 {for (i = 1; i <=NF; i++)
tag[i]=$i
print "<" node "XML>"}
NR != 1 {print " <" node ">"
for (i = 1; i <= NF; i++)
print " <" tag[i] ">" $i "</" tag[i] ">"
print " </" node ">"}
END {print "</" node "XML>"}

Script Execution Command
-------------------------

nawk -v node=whatever -f pj.awk pj.csv

Output :
--------

<whateverXML>
<whatever>
</whatever>
<whatever>
<"Store>"1",</"Store>
<Number",>"35%",</Number",>
<"Quarter>"29%",</"Quarter>
<1>"59%",</1>
<Sales",>"42%"</Sales",>
</whatever>
<whatever>
<"Store>"2",</"Store>
<Number",>"61%",</Number",>
<"Quarter>"37%",</"Quarter>
<1>"34%",</1>
<Sales",>"86%"</Sales",>
</whatever>
<whatever>
<"Store>"3",</"Store>
<Number",>"68%",</Number",>
<"Quarter>"47%",</"Quarter>
<1>"62%",</1>
<Sales",>"18%"</Sales",>
</whatever>
<whatever>
<"Store>"4",</"Store>
<Number",>"78%",</Number",>
<"Quarter>"83%",</"Quarter>
<1>"91%",</1>
<Sales",>"78%"</Sales",>
</whatever>
<whatever>
<"Store>"5",</"Store>
<Number",>"53%",</Number",>
<"Quarter>"44%",</"Quarter>
<1>"67%",</1>
<Sales",>"56%"</Sales",>
</whatever>
</whateverXML>

Also, please could you explain me the steps, Sir, so I can understand better.

Thanks once again for your help.

pjanakir
# 9  
Old 09-10-2005
Input CSV file to get Output!

Sir,
I need the Input file to be a .CSV file so that I could get the proper output. Could you please help me?

Thanks,
pjanakir
# 10  
Old 09-10-2005
assuming you don't have any embedded commas within the quoted fields.....

pj.awk
Code:
BEGIN {FS=",";OFS = "\n"}
NR == 1 {for (i = 1; i <=NF; i++)
            tag[i]=$i
         print "<" node "XML>"}
NR != 1 {print "   <" node ">"
         for (i = 1; i <= NF; i++)
            print "      <" tag[i] ">" $i "</" tag[i] ">"
         print "   </" node ">"}
END {print "</" node "XML>"}

# 11  
Old 09-10-2005
Thanks a million, my friend!

Sir,
I don't have words to thank you. But I want to understand it, so I can explain it to my supervisor. Can you go ahead and explain the .awk script and the execution command too? I would appreciate if you could do so? I thought awk was just a command used to search for patterns, but never knew that we could also use for file conversion. I really did not understand, what you meant by FS, OFS, NR, NF, node etc. Can you please go thru in detail step by step?

I really thank you once again.

Thanks,
pjanakir
# 12  
Old 09-10-2005
Code:
BEGIN {
      # input Field Separator set to ','
   FS=",";
      # output Field Separator set to '\n' [new line]
   OFS = "\n"}

NR == 1 {
            # for the FIRST record/line in a file - iterate though the fields 
            # from '1' to 'NF' [NF stands for 'Number_of_Fields'
         for (i = 1; i <=NF; i++)
                # populate an array 'tag' indexed by the field NUMBER with the
                # VALUE of the field - here're we're reading the HEADERS
            tag[i]=$i

             # print the BEGINNING of the XML document tagged with the value
             # with the value of the variable 'node' passed into the script
         print "<" node "XML>"
}
   # for the other records/lines - other than the FIRST line.....
NR != 1 {
             # this is the BEGINNING of the of the XML record
         print "   <" node ">"
             # iterate though the fields........ "i" is the iterator
         for (i = 1; i <= NF; i++)
              # outputing the header for this field "tag[i]" and the VALUE of this
              # of this field "$i"
            print "      <" tag[i] ">" $i "</" tag[i] ">"

            # this is the END of the XML record
         print "   </" node ">"}
    # at the end of the file processing CLOSE the entire XML document
END {
   print "</" node "XML>"
}

do 'man nawk' for the meaning of the builtin variables [FS, OFS, NF etc...] and the syntax of the language.

Last edited by vgersh99; 09-11-2005 at 12:10 AM..
# 13  
Old 09-11-2005
Thanks!

Thanks a lot.
# 14  
Old 09-15-2005
Unix Command for Jar files!

Hi vgersh99,
Assuming that we don't know what the name of the jar file, we are searching for is, do we have any way, of finding a string or a file in it. Is there any Unix command for it?

thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting XML to CSV

Hello, For i while i have been using XMLStarlet to convert several XML files to CSV files. So far this always went fine. Today however i got a new XML format however but i cannot find out how to get the data i need. Below is part of the code where it shows the different format. What... (10 Replies)
Discussion started by: SDohmen
10 Replies

2. UNIX for Beginners Questions & Answers

Xml to csv (again)

Hello, I have copied .xml code for a single item below. I am trying to extract three items (field indices*b244 (second occurrence), b203, and j151), so the desired output would be: 9780323013543 Manual of Natural Veterinary Medicine: Science and Tradition, 1e 68.95 A parallel solution,... (14 Replies)
Discussion started by: palex
14 Replies

3. UNIX and Linux Applications

Xml to csv

Hello, Does anyone know of a way to convert an .xml file (ONIX) to something more workable, like a .csv (or even .xls) file? Ideally something on the command line would be ideal, but not absolutely necessary. I would be dealing with .xml files of 125 MB+. I am using XQuartz in El Capitan. ... (17 Replies)
Discussion started by: palex
17 Replies

4. Shell Programming and Scripting

XML to CSV

I want to pharse below Xml Using Shell Scripting . Thanks in Advance <md> <neid> <neun>1523</neun> <nedn>XXX1212</nedn> <nesw>fffff12515</nesw> </neid> <mi> <mts>20141128001500</mts> <gp>550</gp> <mt>pmct1</mt> <mt>pmNo2</mt> <mt>pmNo3S</mt> <mv> <moid>Ma=1,Rn=1,Ul=311C</moid>... (6 Replies)
Discussion started by: pareshkp
6 Replies

5. Shell Programming and Scripting

How to convert xml to csv ?

I am in need of converting billions of XML into csv file to load data to DB, i have found the below code in perl but not sure why it's not working properly. CODE: #!/usr/bin/perl # Script to illustrate how to parse a simple XML file # and pick out all the values for a specific element, in... (1 Reply)
Discussion started by: rspwilliam
1 Replies

6. Shell Programming and Scripting

Convert xml to csv

I need to convert below xml code to csv. I searched other posts as well but this post (_https://www.unix.com/shell-programming-scripting/174417-extract-parse-xml-data-statistic-value-csv.html) gives "sed command garbled" error. As of now I have written a long script to do it, but can it be done with... (7 Replies)
Discussion started by: dineshydv
7 Replies

7. Shell Programming and Scripting

XML to CSV specific

Hi , Please any one to help on ,extract this xml code into csv columns list. <SOURCEFIELD BUSINESSNAME ="" DATATYPE ="date" DESCRIPTION ="" FIELDNUMBER ="1" FIELDPROPERTY ="0" FIELDTYPE ="ELEMITEM" HIDDEN ="NO" KEYTYPE ="NOT A KEY" LENGTH ="19" LEVEL ="0" NAME ="BUSINESS_DATE"... (4 Replies)
Discussion started by: mohan705
4 Replies

8. Shell Programming and Scripting

XML to csv transformation

Hi, I want to write a perl script. Which should accept the xml file, one xsl file and the loaction. The perl script should process the xml file using the xsl file and puts the out put in specified location. For example: My.perl is perls cript. my.xml is like this <?xml version="1.0"... (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

9. Shell Programming and Scripting

CSV processing to XML

Hi, i am really fresh with shell scripting and programming, i have an issue i am not able to solve to populate data on my server for Cisco IP phones. I have CSV file within the following format: ;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;... (9 Replies)
Discussion started by: angel2008
9 Replies

10. Shell Programming and Scripting

Help to convert XML to CSV

Apologies if this has already been covered in this site somewhere, I did try looking but without any success. I am new to the whole XML thing, very late starter, and have a requirement to convert an XML fiule to a CSV fomat. I am crrently working on a Solaris OS. Does anyone have any suggestions,... (2 Replies)
Discussion started by: rossingi_33
2 Replies
Login or Register to Ask a Question