Visit The New, Modern Unix Linux Community


XML to Text file Parsing Using shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML to Text file Parsing Using shell scripting
# 1  
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.
Code:
<xn:SubNetwork id="SNJNPRZDCR0R03">
                <xn:MeContext id="PRSJU0005">
                    <xn:VsDataContainer id="PRSJU0005">
                        <xn:attributes>
                            <xn:vsDataType>vsDataMeContext</xn:vsDataType>
                            <xn:vsDataFormatVersion>EricssonSpecificAttributes.11.03</xn:vsDataFormatVersion>
                            <es:vsDataMeContext>
                                <es:userLabel>PRSJU0005</es:userLabel>
                                <es:ipAddress>166.206.252.42</es:ipAddress>
                                <es:neMIMversion>vN.1.4</es:neMIMversion>
                                <es:lostSynchronisation>SYNCHRONISED</es:lostSynchronisation>
                                <es:rbsIubId>214</es:rbsIubId>
                                <es:multiStandardRbs6k>false</es:multiStandardRbs6k>
                                <es:mirrorMIBversion>N.1.12.K.1.3</es:mirrorMIBversion>
                            </es:vsDataMeContext>
                        </xn:attributes>
                    </xn:VsDataContainer>
                </xn:MeContext>
    </xn:SubNetwork>
<xn:SubNetwork id="LSVLKY53CR0R02">
                <xn:MeContext id="KYLSU1005">
                    <xn:VsDataContainer id="KYLSU1005">
                        <xn:attributes>
                            <xn:vsDataType>vsDataMeContext</xn:vsDataType>
                            <xn:vsDataFormatVersion>EricssonSpecificAttributes.11.03</xn:vsDataFormatVersion>
                            <es:vsDataMeContext>
                                <es:userLabel>KYLSU1005</es:userLabel>
                                <es:ipAddress>107.71.129.250</es:ipAddress>
                                <es:neMIMversion>vN.1.4</es:neMIMversion>
                                <es:lostSynchronisation>SYNCHRONISED</es:lostSynchronisation>
                                <es:rbsIubId>1005</es:rbsIubId>
                                <es:multiStandardRbs6k>false</es:multiStandardRbs6k>
                                <es:mirrorMIBversion>N.1.12.K.1.3</es:mirrorMIBversion>
                            </es:vsDataMeContext>
                        </xn:attributes>
                    </xn:VsDataContainer>
                </xn:MeContext>
    </xn:SubNetwork>

The Desired Output should be in a text file eg:
Code:
SubNetwork id    MeContext id    ipAddress
SNJNPRZDCR0R03    PRSJU0005    166.206.252.42
LSVLKY53CR0R02    KYLSU1005    107.71.129.250

Any Help would be greatly Appreciated.

Last edited by Franklin52; 04-10-2012 at 07:22 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Do you want just three fields from the entire file? Based on the sample file u have given, you have picked up only Subnet, Context and IP Address.
# 3  
yes losttouch, i only want those three fields.

Thanks//
# 4  
Install perl XML::XPath module
Code:
# yum install perl-XML-XPath.noarch
or
# perl -MCPAN -e 'install XML::XPath'

Try this:
Code:
#! /usr/bin/perl
use XML::XPath;
$,=" ";

$xp=XML::XPath->new($ARGV[0]);
$nodes=$xp->find('//xn:SubNetwork');
foreach $i ($nodes->get_nodelist) {
        $sn=$i->getAttribute("id");
        foreach $j ($i->find('xn:MeContext')->get_nodelist) {
                $mc=$j->getAttribute("id");
                foreach $k ($j->find('xn:VsDataContainer/xn:attributes/es:vsDataMeContext/es:ipAddress/text()')->get_nodelist) {
                        print $sn, $mc, $k->getNodeValue(),"\n";
                }
        }
}

This User Gave Thanks to chihung For This Post:
# 5  
try xmllint

check out
openSUSE Lizards

Have a look at section under "Using Namespaces in XPath Expressions"
# 6  
Code:
nawk -F"[\"\>\<]" 'BEGIN{print "SubNetwork id    MeContext id    ipAddress"} /SubNetwork id/{a=$3} /userLabel/{b=$3}/ipAddress/{print a,b,$3}' input.txt

# 7  
Thank you itkamaraj , the command worked like a charm.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #745
Difficulty: Medium
The Austrian Computer Kit (ACK) is a retargetable compiler suite and toolchain written by Andrew Tanenbaum.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

How to extract data from XML file using shell scripting?

Hi , I have input file as XML. following are input data #complex.xml Code: <?xml version="1.0" encoding="UTF-8"?><TEST_doc xmlns="http://www.w3.org/2001/XMLSchema-instance"> <ENTRY uid="123456"> <protein> <name>PROT001</name> <organism>Human</organism> ... (1 Reply)
Discussion started by: arun_kohan
1 Replies

4. Shell Programming and Scripting

How to extract data from xml file using shell scripting?

Hi evry1, This is my 1st post in this forum.Pls help me I want to extract some data froma xml file which has 2000 lines using shell scripting. Actually my xml file has some "audio and video codes" which i need to arrange in a column wise format after extracting it using shell scripting.I... (4 Replies)
Discussion started by: arun_kohan
4 Replies

5. Shell Programming and Scripting

Scripting Language for Parsing XML

Hello, I want to know which shell scripting language (like AWk, Perl or any other) is the best for parsing and editing 1) space or Comma Separated Datsets 2) XML type Datasets. (1 Reply)
Discussion started by: ajayram
1 Replies

6. Shell Programming and Scripting

XML: parsing of the Google contacts XML file

I am trying to parse the XML Google contact file using tools like xmllint and I even dived into the XSL Style Sheets using xsltproc but I get nowhere. I can not supply any sample file as it contains private data but you can download your own contacts using this script: #!/bin/sh # imports... (9 Replies)
Discussion started by: ripat
9 Replies

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

8. UNIX for Dummies Questions & Answers

Delete a record in a xml file using shell scripting

find pattern, delete line with pattern and 3 lines above and 8 lines below the pattern. The pattern is "isup". The entire record with starting tag <record> and ending tag </record> containing the pattern is to be deleted and the rest to be retained. <record> ... (4 Replies)
Discussion started by: sdesstp
4 Replies

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

10. Shell Programming and Scripting

Plz Help To convert xml file to text file using bourn shell scripts

If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert xml into text file(fixed length) using Unix Borne shell scripts. My xml file: <root> <header_rec recordtype="00"> <record_id>00</record_id> <country_code>AK></country_code>... (0 Replies)
Discussion started by: ram2s2001
0 Replies

Featured Tech Videos