Find the answer to your Linux question:
Results 1 to 5 of 5
i need to parse the following output and get all the lines representing a particular row the output is : <resultSet> <row> <column xsi:type="Key"> <id>145</id> <policy className="Meter" id="144"/> <name>key1</name> <description>key</description> ...
  1. #1
    Just Joined!
    Join Date
    Feb 2010
    Posts
    14

    parsing output

    i need to parse the following output and get all the lines representing a particular row

    the output is :

    <resultSet>
    <row>
    <column xsi:type="Key">
    <id>145</id>
    <policy className="Meter" id="144"/>
    <name>key1</name>
    <description>key</description>
    <type>SESSION</type>
    </column>
    </row>
    <row>
    <column xsi:type="Key">
    <id>150</id>
    <policy className="Meter" id="149"/>
    <name>key2</name>
    <description>first key</description>
    <type>SESSION</type>
    </column>
    </row>
    </resultSet>

    Now I need to get the row based on the <id>
    So I know the id say 145, now I need to print out all the elements in that row.
    Please help me out with this.I need to do this in shell script


    Thanks in advance

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Sounds like a homework problem - "I need to do this in shell script"
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    Feb 2010
    Posts
    14
    do u have an idea on how to do it?

  4. #4
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    what language you gonna write it in?
    linux user # 503963

  5. #5
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    After looking at your previous posts, you don't seem to be a guy doing homeworks.

    The following script is not a general purpose xml bash parser, it just does what you want. C++/C, Java... have got full XML processors and parsers for real flexibility and productivity.

    The idea is to write all elements of a row on one line, then select the required line by the id tag, and finally present the data as in the original file.

    Code:
    #!/bin/bash
    IN="/var/tmp/xmlforbash.xml"
    OUT="/var/tmp/toRows.xml"
    rm -f "$OUT"
    row=""
    while read line
    do
      [ "$line" != "<resultSet>" ] && row="$row$line"
      if [ "$line" = "</row>" ]; then
        echo "$row" >> "$OUT"
        row=""
      fi
    done < "$IN"
    grep "<id>145</id>" "$OUT" | sed 's/></>\n</g'
    #rm -f "$OUT"
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...