XML to CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML to CSV
# 1  
Old 12-01-2014
XML to CSV

I want to pharse below Xml Using Shell Scripting .

Thanks in Advance

Code:
<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>
<r>11</r>
<r>21</r>
<r>0</r>
</mv>
<mv>
<moid>Ma=1,Rn=1,Ul=311B</moid>
<r>4</r>
<r>11</r>
<r>0</r>
</mv>
<mv>
<moid>Ma=1,Rn=1,Ul=347C</moid>
<r>11</r>
<r>43</r>
<r>1</r>
</mv>
</mi>
</md>



Output:-
UC          pmct1          pmNo2          pmNo3S
311C	     11	        21		   0
311B	     4	        11                  0	
347C	     11	        43		   1

---------- Post updated at 03:41 PM ---------- Previous update was at 03:39 PM ----------

Quote:
Originally Posted by pareshkp
I want to pharse below Xml

Code:
<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>
<r>11</r>
<r>21</r>
<r>0</r>
</mv>
<mv>
<moid>Ma=1,Rn=1,Ul=311B</moid>
<r>4</r>
<r>11</r>
<r>0</r>
</mv>
<mv>
<moid>Ma=1,Rn=1,Ul=347C</moid>
<r>11</r>
<r>43</r>
<r>1</r>
</mv>
</mi>
</md>



Output:-
UC          pmct1          pmNo2          pmNo3S
311C	     11	        21		   0
311B	     4	        11                  0	
347C	     11	        43		   1

# 2  
Old 12-01-2014
What have you tried?
# 3  
Old 12-01-2014
I have tried Below but its not workingSmilie

Code:
awk -F'[\"\>\<]' -v OFS=',' 'BEGIN{print "''" } /pmct1/{a=$3} /pmNo2/{b=$3}/pmNo3S/{print a,b,$3}' $XXX > $test

# 4  
Old 12-01-2014
What is $XXX ?
# 5  
Old 12-01-2014
This works...

awk -fx.awk <input-xml-file>

where x.awk contains....

Code:
BEGIN {printf("%-10s%-10s%-10s%-10s\n","UC","pmct1","pmNo2","pmNo3S")}
/<moid>/ {
        split($0,A,"="); split(A[4],B,"<"); UC= B[1]
        getline; split($0,A,"<");split(A[2],B,">");pmct1=B[2]
        getline; split($0,A,"<");split(A[2],B,">");pmNo2=B[2]
        getline; split($0,A,"<");split(A[2],B,">");pmNo3S=B[2]
        printf("%-10s%-10s%-10s%-10s\n",UC,pmct1,pmNo2,pmNo3S)
        }

I think $XXX is the forbidden variable of which we do not speak (kidding)
This User Gave Thanks to blackrageous For This Post:
# 6  
Old 12-02-2014
Hello pareshkp,

Following may hepl you also in same.

Code:
awk 'BEGIN{OFS="\t";print "UC" OFS "pmct1" OFS "pmNo2" OFS "pmNo3S"} /\<moid\>/ {match($0,/Ul.*\</);A=substr($0,RSTART+3,RLENGTH-5);X[++o]=A;getline;{while($0 !~ /<\/mv>/){match($0,/[0-9]+/);X[o]=X[o] OFS substr($0,RSTART,RLENGTH);getline}}} END{for(i in X){print X[i]}}' OFS="\t"  Input_file

Output will be as follows.
Code:
UC      pmct1   pmNo2   pmNo3S
311C    11      21      0
311B    4       11      0
347C    11      43      1

In this solution I have hardcoded 1st line of UC pmct1 pmNo2 pmNo3S As I haven't seen any where about UC in code. Kindly let us know if this helps.

EDIT: Adding a non-single liner form of same solution, you can make use it as a script as well.

Code:
awk '
BEGIN{OFS="\t";print "UC" OFS "pmct1" OFS "pmNo2" OFS "pmNo3S"}
/\<moid\>/ {
match($0,/Ul.*\</);
A=substr($0,RSTART+3,RLENGTH-5);
X[++o]=A;
getline;{
                while($0 !~ /<\/mv>/){
                                        match($0,/[0-9]+/);
                                        X[o]=X[o] OFS substr($0,RSTART,RLENGTH);getline
                                     }
        }
           }
END{for(i in X){print X[i]}
}' OFS="\t" xml_test ##### xml_test is input file

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-02-2014 at 11:25 AM.. Reason: Added non single liner code for same
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 12-02-2014
Also try :

Code:
awk -F'[><=]' '/^<moid/{k=1; if(p)print p; p=$6}k && /^<r/{p=p OFS $3}END{print p}' infile

-- edit--

With header

Code:
awk -F'[><=]' 'FNR==1{print "UC","pmct1","pmNo2","pmNo3S"}/^<moid/{k=1; if(p)print p; p=$6}k && /^<r/{p=p OFS $3}END{print p}' OFS="\t" infile


Last edited by Akshay Hegde; 12-02-2014 at 01:29 PM..
This User Gave Thanks to Akshay Hegde For This Post:
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

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

5. 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

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

CSV to XML

Iam pretty new to UNIX and would like to convert a CSV to an XML file using AWK scripts. Can anybody suggest a solution? My CSV file looks something like this : Serial No Growth% Annual % Commission % Unemployed % 1 35% 29% 59% 42% 2 61% ... (15 Replies)
Discussion started by: pjanakir
15 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