XML to Text file Parsing Using shell scripting


 
Thread Tools Search this Thread
Operating Systems Solaris XML to Text file Parsing Using shell scripting
# 1  
Old 05-07-2012
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 :
Code:
<gn:ExternalGsmCell id="016P3A">
                <gn:attributes>
                    <gn:mnc>410</gn:mnc>
                    <gn:mcc>310</gn:mcc>
                    <gn:lac>8016</gn:lac>
                    <gn:bcchFrequency>677</gn:bcchFrequency>
                    <gn:ncc>3</gn:ncc>
                    <gn:bcc>4</gn:bcc>
                    <gn:userLabel>016P3A</gn:userLabel>
                    <gn:cellIdentity>50031</gn:cellIdentity>
                </gn:attributes>
                <xn:VsDataContainer id="016P3A">
                    <xn:attributes>
                        <xn:vsDataType>vsDataExternalGsmCell</xn:vsDataType>
                        <xn:vsDataFormatVersion>EricssonSpecificAttributes.11.03</xn:vsDataFormatVersion>
                        <es:vsDataExternalGsmCell>
                            <es:maxTxPowerUl>100</es:maxTxPowerUl>
                            <es:qRxLevMin>100</es:qRxLevMin>
                            <es:individualOffset>0</es:individualOffset>
                            <es:bandIndicator>1</es:bandIndicator>
                            <es:parentSystem>SubNetwork=ONRM_RootMo_R,vsDataExternalGsmPlmn=GSMPLMN_310_410_3</es:parentSystem>
                            <es:mncLength>3</es:mncLength>
                        </es:vsDataExternalGsmCell>
                    </xn:attributes>
                </xn:VsDataContainer>
</gn:ExternalGsmCell>
............

Pattern continues for diff Id's

The output should be like

Code:
GsmCell id    mnc    mcc    lac    bcchFrequency    ncc    bcc    cellIdentity    maxTxPowerUl    qRxLevMin    individualOffset    bandIndicator    mncLenght
016P3A        410    310    8016    677        3    4    50031        100        100        0            1        3

Any Help would be greatly appreciated.

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 05-07-2012 at 05:39 AM..
# 2  
Old 05-07-2012
i put only to find three values. for other values, just extend the awk

Code:
 
$ nawk -F"[\"<>]" '/ExternalGsmCell id/{id=$3}/mnc/{m=$3}/mcc/{mc=$3} {if(id && m && mc){print id,m,mc; id=m=mc=0;}}' input.txt
016P3A 410 310

# 3  
Old 05-07-2012
wow that was really fast. if time permits i need one more help below is one more xml file with diff pattern i tried it but dint get it , iam sure its a peice of cake for you.

Code:
<xn:MeContext id="LSVLKY001">
                    <xn:ManagedElement id="1">
                        <un:RncFunction id="1">
                            <un:UtranCell id="KYLSU17A">
                                <un:attributes>
                                    <un:localCellId>10071</un:localCellId>
                                    <un:uarfcnUl>862</un:uarfcnUl>
                                    <un:uarfcnDl>1087</un:uarfcnDl>
                                    <un:primaryScramblingCode>361</un:primaryScramblingCode>
                                    <un:primaryCpichPower>315</un:primaryCpichPower>
                                    <un:maximumTransmissionPower>416</un:maximumTransmissionPower>
                                    <un:primarySchPower>-18</un:primarySchPower>
                                    <un:cId>10071</un:cId>
                                    <un:secondarySchPower>-35</un:secondarySchPower>
                                    <un:bchPower>-31</un:bchPower>
                                    <un:lac>15913</un:lac>
                                    <un:rac>13</un:rac>
                                    <un:sac>10071</un:sac>
                </un:attributes>
                                <xn:VsDataContainer id="KYLSU17A">
                                    <xn:attributes>
                                    <es:cellReserved>1</es:cellReserved>
                                    <es:administrativeState>1</es:administrativeState>
                                   </xn:attributes>
                                </xn:VsDataContainer>
                            </un:UtranCell>
            </un:RncFunction>
                    </xn:ManagedElement>
                </xn:MeContext>

The xml file has the following lines in the file
There are n number of Mecontext id loops
there are y number of utrancell id loops in each Mecontext loop.
I deleted some unwanted data. i want the output to be in the following format

Code:
RNC   UtranCell   cId   uarfcnUl   uarfcnDl   primaryScramblingCode   primaryCpichPower   maximumTransmissionPower   primarySchPower   secondarySchPower   bchPower   lac   rac   sac   cellReserved   administrativeState 
                             LSVLKY53RNC01   KYLSUTOY6552C   65523   862   1087   3   337   446   -18   -35   -31   15913   13   65523   1   1       
LSVLKY53RNC01   KYLSUTOY6552B   65524   862   1087   3   337   446   -18   -35   -31   15913   13   65523   1   1       
LSVLKY53RNC01 TNLSUTOY2018T 20183   862   1087   3   337   446   -18   -35   -31   15913   13   65523   1   1       
SNJNLIDCR0R03   PRSJUTOY0051Y   51   812   1037   246   337   446   -18   -35   -31   51799   255   51   1   1
SNJNLIDCR0R03   PRSJUTOY0051X   51   812   1037   246   337   446   -18   -35   -31   51799   255   51   1   1


Last edited by tech_frk; 05-07-2012 at 05:08 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

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

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
Login or Register to Ask a Question