Sponsored Content
Top Forums Shell Programming and Scripting Using shell command need to parse multiple nested tag value of a XML file Post 302882919 by Akshay Hegde on Thursday 9th of January 2014 04:01:59 PM
Old 01-09-2014
You may try, might not be intelligent as one Corona posted, but for given sample it works.
Code:
awk '
    !f && /<(.*)>$/{
                     s=$0; f=g=1
                     gsub(/</,"</",s)
                     next
                   }
                 f{
                     g=(/<(.*)>/ && !/<\/(.*)>$/)?0:g
                     if(/\<(.*)>(.*)<\/(.*)>/){
                                                  gsub(/[<>]|<\//,"|")
                                                  split($0,A,"|")
                                                  com = g == 1 ? com ? com OFS A[3] : A[3] : com
                                                  dat = g == 0 ? dat ? dat OFS A[3] : A[3] : dat 
                                                  hea = m ? hea : hea ? hea OFS A[2]"_CDATA" : A[2]"_CDATA"
                                              }
                      if(/<\/(.*)>$/ && $0!~s){
                                             ++d
                                             if(d==count){
                                                           print m ? com OFS dat : hea RS com OFS dat
                                                           dat="";d=0;m=1
                                                         }  
                                              }
                   }
               $0~s{
                       f=s=com=d=""
                   }
     ' count="1" OFS="\t" test.xml

Code:
$ cat test.xml
<gp>
    <mms>1110012</mms>
    <tg>988</tg>
    <mm>LongTime</mm>
    <lv>
        <lkid>StartEle=ONE, Desti = Motion</lkid>
        <kk>12</kk>
    </lv>
    <lv>
        <lkid>StartEle=ONE, Source = Velocity</lkid>
        <kk>2</kk>
    </lv>
    <lv>
        <lkid>StartEle=ONE, Source = Park</lkid>
        <kk>2</kk>
    </lv>
</gp>

<gp>
    <mms>2221100</mms>
    <tg>989</tg>
    <mm>LongVelocity</mm>
    <lv>
        <lkid>StartEle=ONE, Source = Velocity</lkid>
        <kk>772</kk>
    </lv>
    <lv>
        <lkid>StartEle=ONE, Desti = Motion</lkid>
        <kk>900</kk>
    </lv>
    <lv>
        <lkid>StartEle=ONE, Source = Park</lkid>
        <kk>2</kk>
    </lv>
</gp>

Code:
mms_CDATA  tg_CDATA  mm_CDATA       lkid_CDATA            kk_CDATA
1110012    988       LongTime        StartEle=ONE, Desti = Motion       12
1110012    988       LongTime        StartEle=ONE, Source = Velocity    2
1110012    988       LongTime        StartEle=ONE, Source = Park        2
2221100    989       LongVelocity    StartEle=ONE, Source = Velocity    772
2221100    989       LongVelocity    StartEle=ONE, Desti = Motion       900
2221100    989       LongVelocity    StartEle=ONE, Source = Park        2

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

big xml file with nested loop parse

I have an xml file with the structure: <tag1> <value1>xyx</value1> <value2>123</value2> </tag1> <tag1> <value1>568</value1> <value2>zzzzz</value2> </tag1> where I want to parse each data pair in the this single file, so something like: find first tag1 data pair... (1 Reply)
Discussion started by: unclecameron
1 Replies

2. Shell Programming and Scripting

shell command to remove some XML tag is needed

Hi all, I have a file which i have to remove some line from it, the lines that i have to remove from my file is as below: </new_name></w"s" langue="Fr-fr" version="1.0" encoding="UTF-8" ?> <New_name> and it is finding at the middle of my file, is there any command line in linux to do it or do... (10 Replies)
Discussion started by: id_2pc
10 Replies

3. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

4. Shell Programming and Scripting

awk Script to parse a XML tag

I have an XML tag like this: <property name="agent" value="/var/tmp/root/eclipse" /> Is there way using awk that i can get the value from the above tag. So the output should be: /var/tmp/root/eclipse Help will be appreciated. Regards, Adi (6 Replies)
Discussion started by: asirohi
6 Replies

5. Emergency UNIX and Linux Support

Trying to parse a xml file for only one tag

I have a xml file in where I need to parse only a particular tag and print the output in the shell script. Here is the tag info in the xml file <dp:file> This is dp file output </dp:file> Output should be printed as This is dp file output. Please help.Thank you. (5 Replies)
Discussion started by: chandu123
5 Replies

6. Shell Programming and Scripting

XML Parse between to tag with upper tag

Hi Guys Here is my Input : <?xml version="1.0" encoding="UTF-8"?> <xn:MeContext id="01736"> <xn:VsDataContainer id="01736"> <xn:attributes> <xn:vsDataType>vsDataMeContext</xn:vsDataType> ... (12 Replies)
Discussion started by: pareshkp
12 Replies

7. Shell Programming and Scripting

Creating multiple xml tag lines in a file

Hi All, Can someone tell me how can we create same xml tag lines based on the number of lines present in other file and replace the Name variable vaule present in other file. basically I have this xml line <typ:RequestKey NameType="RIC" Name="A1" Service="DDA"/> and say I... (4 Replies)
Discussion started by: Optimus81
4 Replies

8. Shell Programming and Scripting

XML files with spaces in the tag name, parse & display?

Greetings all, I have an XML file that is being generated from my application, here is a sample of the first tag (That I am trying to remove and display in a list..) Example- <tag one= "data" data="1234" updateTime="1300"> <tag one= "data1" data="1234" updateTime="1300"> <tag... (5 Replies)
Discussion started by: jeffs42885
5 Replies

9. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

10. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 10:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy