BASH script to parse XML and generate CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH script to parse XML and generate CSV
# 1  
Old 04-15-2013
BASH script to parse XML and generate CSV

Hi All,

Hope all you are doing good! Need your help. I have an XML file which needs to be converted CSV file. I am not an expert of awk/sed so your help is highly appreciated!!

XML file looks like this:

Code:
<l:event dateTime="2013-03-13 07:15:54.713" layerName="OSB" processName="ABC" eventType="END" eventStatus="SUCCESS" eventCode="" outboundServiceName="" serverHostIP="*******" applicationID="someapp" providerID="****" originatorIP="A.B.C.D" SOAConsumerTransactionID="" SOATransactionID="2f9bbaf6-ae21-41ab-925a-998a5efd82a0"><xml:changeEligibleTariff_1Response xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xml="http://soa.o2.ie/xml__1_0"><xml:result><cor:statusCode xmlns:cor="http://soa.o2.ie/coredata_1_0">mt-22-6001-W</cor:statusCode><cor:externalDescription xmlns:cor="http://soa.o2.ie/coredata_1_0">Partial Success.The Request is under process.</cor:externalDescription></xml:result><xml:tariffCode>52</xml:tariffCode><xml:transactionId>5850034</xml:transactionId><xml:msisdn>353867877726</xml:msisdn><xml:subscriberType>PREPAY</xml:subscriberType><xml:accountNumber>229951060</xml:accountNumber><xml:channelIdentifier>SOMEAPP</xml:channelIdentifier><xml:agentIdentifier>SOMEAPP</xml:agentIdentifier></xml:changeEligibleTariff_1Response>
</l:event>

and the generated CSV looks like:

Code:
2013-03-13 07:15:54.713,2f9bbaf6-ae21-41ab-925a-998a5efd82a0,52,5850034,229951060,mt-22-6002-W,Partial Success.The Request is under process.,SOMEAPP

Thanks in advance.

Regards,
Bhaskar
# 2  
Old 04-15-2013
You can use awk to extract required attributes and values.

Here is a code that extracts the first 3 required fields:
Code:
awk -F'[=>]' ' {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( $i ~ /dateTime/ )
                        {
                                dT = $( i + 1 )
                                gsub (/\"[ ]+.*|\"/, X, dT)
                        }
                        if ( $i ~ /SOATransactionID/ )
                        {
                                SID = $( i + 1 )
                                gsub (/\">.*|\"/, X, SID)
                        }
                        if ( $i ~ /<xml:tariffCode/ )
                        {
                                tC = $( i + 1 )
                                gsub (/<.*/, X, tC)
                        }
                }
} END {
        print dT, SID, tC
} ' OFS=, xmlfile

I will leave it to you for extracting the rest.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 04-17-2013
Hi Yoda,

Thanks a lot for your effort. That definitely will make a help.

Regards,
Bhaskar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to generate .csv file

Dears,I need your help in this, I have to create a report based on the output file generated by another program. I want to write a shell script for this. The output file generated every 15 minutes but i can’t open it until the end of day so the script will get the file as an input the file will be... (8 Replies)
Discussion started by: abdul2020
8 Replies

2. Shell Programming and Scripting

Script to generate csv file

Dears, I am new in shell world and I need your help in this, I have to create a report based on the output file generated by another program. I want to write a shell script for this. The output file generated every 15 minutes but i can’t open it until the end of day so the script will get the... (3 Replies)
Discussion started by: abdul2020
3 Replies

3. Programming

How to write a java program that will parse through an XML file and generate a report?

I'm pretty new to Java and I am trying to write a program that will pick up a file from a windows directory adn parse through the XML file to produce a report that will show a total item count and a total paid amount. Any one have any suggestions? Trying to figure out where to start... (4 Replies)
Discussion started by: risarose87
4 Replies

4. Shell Programming and Scripting

Extract and parse XML data (statistic value) to csv

Hi All, I need to parse some statistic data from the "measInfo" -eg. 25250000 (as highlighted) and return the result into line by line, and erasing all other unnecessary info/tag. Thought of starting with grep "measInfoID="25250000" but this only returns 1 line. How do I get all the output... (8 Replies)
Discussion started by: jackma
8 Replies

5. Shell Programming and Scripting

Script to generate csv file

Hello; I need to generate a csv file that contains a list of all the files in a particular server (from the root directory ie: \) that have a permission stamp of 777. I would like to create the csv so that it contains the following: server name, file name, full path name where file exists,... (17 Replies)
Discussion started by: gvolpini
17 Replies

6. Shell Programming and Scripting

How to read and parse the content of csv file containing # as delimeter into fields using Bash?

#!/bin/bash i=0 cat 1.csv | while read fileline do echo "$fileline" IFS="#" flds=( $fileline ) nrofflds=${#flds} echo "noof fields$nrofflds" fld=0 while do echo "noof counter$fld" echo "$nrofflds" #fld1="${flds}" trying to store the content of line to fields but i... (4 Replies)
Discussion started by: barani75
4 Replies

7. Shell Programming and Scripting

Need to Parse XML from bash script

I am completely new to bash scripting and now need to write a bash script that would parse a XML file and take out values from specific tags. I tried using xsltproc, xml_grep commands. But the issue is that the XML i am trying to parse is not UTF 8. so those commands are unable to parse my XML's... (4 Replies)
Discussion started by: shivashankar.g
4 Replies

8. Shell Programming and Scripting

Facing issue while using xsltproc tp parse XML in bash

I have written a bash script which opens a folder, reads all the *.xml files in it, and pulls the required data that i need from XML tags. I am using xsltproc (my xsl name) (my xml folder location/*.xml) and running this in a for each loop The problem is that some XML files are having special... (3 Replies)
Discussion started by: shivashankar.g
3 Replies

9. Shell Programming and Scripting

Parse XML file into CSV with shell?

Hi, It's been a few years since college when I did stuff like this all the time. Can someone help me figure out how to best tackle this problem? I need to parse a file full of entries that look like this: <eq action="A" sectyType="0" symbol="PGR" exch="CA" curr="VEF" sess="NORM"... (7 Replies)
Discussion started by: Pcushing
7 Replies

10. UNIX for Dummies Questions & Answers

generate xml from a shell script

Hello! I would like to generate an xml file from the output of various commands generated from within a shell script (some will be in CDATA). At the moment the only solution I have come up with is echoing xml tags around the commands eg. echo "<bitism>" >> outputfile /usr/sbin/prtconf... (1 Reply)
Discussion started by: speedieB
1 Replies
Login or Register to Ask a Question