Parsing XML using Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing XML using Shell Script
# 1  
Old 01-18-2010
Power Parsing XML using Shell Script

Hello,

I'm a starting shell scripter and no Perl knowledge. I've trying to do this for a while:
I want to parse an XML file and get certain data out of it and write that data into a CSV file, all this using Shell Scripting (in Bash). Or Perl without any XML Parser/Interpreter (if possible).

I have the following XML file:
Code:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Measurements.xsl" ?>
<!DOCTYPE mdc SYSTEM "Measurements.dtd">
<mdc xmlns:HTML="http://www.w3.org/TR/REC-xml">
  <mfh>
    <st>SUN</st>
    <vn>LIFE</vn>
    <cbt>20091126111000.2+0100</cbt>
  </mfh>
  <md>
    <neid>
      <neun>10101</neun>
      <nedn/>
    </neid>
    <mi>
      <mts>20091126111500.2+0100</mts>
      <gp>999</gp>
      <mt>CAP_LIFE</mt>
      <mt>COMPONENT_SENT</mt>
      <mt>COMPONENT_RECEIVED</mt>
      <mt>SENT</mt>
      <mt>RECEIVED</mt>
      <mt>LINK_LOAD</mt>
      <mt>LINK_LOAD_SENT</mt>
      <mv>
        <moid>Measurements=BoxID=3,CPU=0,HD=7, Source = TCP</moid>
        <r/>
        <r/>
        <r/>
        <r/>
        <r/>
        <r/>
        <r>6.27</r>
        <r>6.07</r>
        <r/>
        <r/>
        <r/>
        <r/>
        <sf>FALSE</sf>
      </mv>
      <mv>
        <moid>Measurements=BoxID=1,CPU=0,HD=5, Source = TCP</moid>
        <r/>
        <r/>
        <r/>
        <r/>
        <r/>
        <r/>
        <r>5.91</r>
        <r>5.91</r>
        <r/>
        <r/>
        <r/>
        <r/>
        <sf>FALSE</sf>
      </mv>
      <mv>
        <moid>Measurements=DEFAULT, Source = TCP</moid>
        <r>193.96</r>
        <r>46993</r>
        <r>23564</r>
        <r>35429</r>
        <r>24520</r>
        <r>196.59</r>
        <r/>
        <r/>
        <r/>
        <r/>
        <r/>
        <r/>
        <r/>
        <sf>FALSE</sf>
      </mv>
      <mv>
        <moid>Measurements=BoxID=5,CPU=0,HD=7, Source = TCP</moid>
        <r/>
        <r/>
        <r/>
        <r/>
        <r/>
        <r>6.21</r>
        <r>5.95</r>
        <r/>
        <r/>
        <r/>
        <r/>
        <r/>
        <sf>FALSE</sf>
      </mv>
      <mv>
        <moid>Measurements=BoxID=8,CPU=0,HD=8, Source = TCP</moid>
        <r/>
        <r/>
        <r/>
        <r/>
        <r/>
        <r>6.21</r>
        <r>0.13</r>
        <r/>
        <r/>
        <r/>
        <r/>
        <sf>FALSE</sf>
      </mv>
    </mi>
  </md>
  <mff>
    <ts>20091126111500.2+0100</ts>
  </mff>
</mdc>

I don't have the XSL and DTD file as written in the XML file.
From this XML file I want to have only the <moid>Measurements=DEFAULT, Source = TCP</moid> and the <r> </r> & <sf> </sf> belonging to it.
And the timestamps in <cbt> </cbt> and <mts> </mts>.
And have these written to a CSV formatted file.

I have this Perl code taken from another Thread (matrixmadhan) but couldn't make anything of it at all (no Perl scripter at all):
Code:
#! /opt/third-party/bin/perl

open(FILE, "<", "a");

while(<FILE>) {
  chomp;
  my @arr = split(/></);
  foreach (@arr) {
    if( />/ && /</ ) {
      if( $. == 1 ) {
        s/(.*)>(.*)<.*$/\1|\2/;
        my($tmp1, $tmp2) = split(/\|/);
        $data .= (" " . $tmp2);
        printf "%s ", $tmp1;
      }
      else {
        s/(.*)>(.*)<.*$/\2/;
        printf "%s ", $_;
      }
    }
  }
  print "\n";
  print "$data\n" if( $. == 1 );
}

close(FILE);

exit 0

If anyone could help me, I'd be grateful.

Last edited by Kage Musha; 01-18-2010 at 10:18 AM.. Reason: Added XSL and DTD part.
# 2  
Old 01-18-2010
Please have a look;
thread 1
thread 2
thread 3
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing custom data into xml in Shell

Hi , I have data as below in a text file { 'AAA' => { 'A1' => 'a1 comment', 'A2' => 'a2 comment' }, 'BBB' => { 'B1' => 'b1 comment' }, 'CCC' => { 'C1' => 'c1 comment', 'C2' => 'c2 comment', 'C3' => 'c3 comment' 'C4' => 'c4... (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

XML-Text Parsing Using shell scripting

HI Guys, I have to parse below xml file :- <xn:SubNetwork id="ONRM_ROOT_MO_R"> <xn:MeContext id="LP101"> <xn:ManagedElement id="1"> <xn:VsDataContainer id="1"> <xn:attributes> ... (8 Replies)
Discussion started by: asavaliya
8 Replies

3. Solaris

XML to Text file Parsing Using shell scripting

Hi, I want to parse an XML File using Shell Script preferably by using awk command, I/P file is : <gn:ExternalGsmCell id="016P3A"> <gn:attributes> <gn:mnc>410</gn:mnc> <gn:mcc>310</gn:mcc> <gn:lac>8016</gn:lac> ... (2 Replies)
Discussion started by: tech_frk
2 Replies

4. Shell Programming and Scripting

Parsing XML using shell script

Well, issue is i have to parse this script to get the VERSION: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleAllowMixedLocalizations</key> ... (9 Replies)
Discussion started by: zorosinister
9 Replies

5. Shell Programming and Scripting

XML to Text file Parsing Using shell scripting

Hi folks, Need some help with XML to text file parsing , the following is the content of the XML File. <xn:SubNetwork id="SNJNPRZDCR0R03"> <xn:MeContext id="PRSJU0005"> <xn:VsDataContainer id="PRSJU0005"> <xn:attributes> ... (6 Replies)
Discussion started by: tech_frk
6 Replies

6. Shell Programming and Scripting

XML parsing using shell script

I have a xml file like this <bul:collectionStrategy name="strategy1"> <bul:collectionTemplateGroup name="15min group"/> <bul:collectionTemplateGroup name="hourly group"/> </bul:collectionStrategy> <bul:CollectionTemplateGroup name="hourly group" > ... (2 Replies)
Discussion started by: LavanyaP
2 Replies

7. Shell Programming and Scripting

Shell script for XML code parsing.

I need to create a shell script that can parse the below XML and send a string back adding all XML values. The text which's not in angular braces are to be printed. Sample code: <RequestBillsRsp... (9 Replies)
Discussion started by: xtatic
9 Replies

8. Shell Programming and Scripting

Shell script for XML code parsing.

Please help me to create a shell script that can parse the below XML and send a string back adding all XML values.:) <RequestBillsRsp... (1 Reply)
Discussion started by: xtatic
1 Replies

9. Shell Programming and Scripting

XML parsing in a shell script.

Below is a XML I have... <?xml version="1.0" encoding="UTF-8" ?> <component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:XXXXX-www-Install-Manifest manifest.xsd" xmlns="urn:qqqqq-Install-Manifest" name="OM" ... (1 Reply)
Discussion started by: dashok.83
1 Replies

10. Shell Programming and Scripting

XML parsing through shell scritps

Hi, Am new to scripting. :) Am trying to figure out whether can i use bash scripting to parse an xml file. Parsing is not just pulling out information according to the pattern but its more of a generic parsing. I should identify the xml hierarchy and pull out information accordingly. It's not a... (2 Replies)
Discussion started by: karthikvela
2 Replies
Login or Register to Ask a Question