Parse XML line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse XML line
# 1  
Old 06-12-2012
Parse XML line

Hi

I am having an xml file with lines like these
PHP Code:
<d name="T2tt_350_100" title="T2tt_012j_350_100_428p4_pPF_PU" add="1" color="4" ls="1" lw="2" normf="1" xsection="0.070152" EqLumi="94651.6" filenames="/user/alkaloge/files/TopTree/7TeV/T2tt_2j_FSim_428p4pPF_S11_PU_350_100/T2tt_2j_FSim_428p4pPF_S11_PU_350_100.root"/>

<
d name="T2tt_500_100" title="T2tt_012j_500_100_428p4_pPF_PU" add="1" color="4" ls="1" lw="2" normf="1" xsection="0.006867" EqLumi="936653.56" filenames="/user/alkaloge/files/TopTree/7TeV/T2tt_2j_FSim_428p4pPF_S11_PU_500_100/T2tt_2j_FSim_428p4pPF_S11_PU_500_100.root"/> 
How can I use bash to parse each argument ie EqLumi, name etc ?

Thanks bunch
# 2  
Old 06-12-2012
I am puzzle with your request to use bash to parse text.
You can use either awk or sed?.
For example with sed:

Code:
sed -e 's/^.*EqLumi="//' file_with_your_xml | sed -e 's/"\s.*//'

# 3  
Old 06-12-2012
Code:
$ cat xmlline.sh
#!/bin/bash

# Remove temp file on exit
trap "rm -f /tmp/$$" EXIT

# Little-known incredibly useful property of xargs:  it handles quotes.
# We can use it to transform x='a' y='b c d' into lines like
#
#       x=a
#       y=b c d
#       ...
#
# Ordinary 'while read' loop can handle that easily, splitting on the first =.
xargs printf '%s\n' < xmldata > /tmp/$$

while IFS="=" read NAME VALUE
do
        END=0
        if [ "${VALUE/\/>/}" != "$VALUE" ]      # Check for /> end-of-token
        then
                END=1
                VALUE="${VALUE/\/>/}"
        fi

        [ "${NAME:0:1}" = "<" ] && NAME="${NAME:1}"     # Remove <

        # Sanity check, don't assign variables when we have no value
        if [ ! -z "$VALUE" ]
        then
                VARS="$VARS $NAME"
                read $NAME <<<"$VALUE" # BASH trick to assign variable name without eval
        fi

        # If we found />, print and clear all variables
        if [ "$END" -gt 0 ]
        then
                echo "Found fields $VARS"
                for X in $VARS
                do
                        printf "$X\t${!X}\n"
                        read $X <<<"" # BASH trick to clear variable name without eval
                done
                echo
                VARS=""
        fi
done < /tmp/$$

exit 0

$ ./xmlline.sh
Found fields  name title add color ls lw normf xsection EqLumi filenames
name    T2tt_350_100
title   T2tt_012j_350_100_428p4_pPF_PU
add     1
color   4
ls      1
lw      2
normf   1
xsection        0.070152
EqLumi  94651.6
filenames       /user/alkaloge/files/TopTree/7TeV/T2tt_2j_FSim_428p4pPF_S11_PU_350_100/T2tt_2j_FSim_428p4pPF_S11_PU_350_100.root

Found fields  name title add color ls lw normf xsection EqLumi filenames
name    T2tt_500_100
title   T2tt_012j_500_100_428p4_pPF_PU
add     1
color   4
ls      1
lw      2
normf   1
xsection        0.006867
EqLumi  936653.56
filenames       /user/alkaloge/files/TopTree/7TeV/T2tt_2j_FSim_428p4pPF_S11_PU_500_100/T2tt_2j_FSim_428p4pPF_S11_PU_500_100.root


$

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse XML For Values

Hi All, I want to parse XML to extract values of the tags to do further processing. The XML looks like <?xml version="1.0" encoding="ISO-8859-1"?> <allinput> <input A="2389906" B="install"> <C>111</C> <D>222</D> <E>333</E> <F></F> <G>444</G> <H></H> <I></I> <J></J> <K>C,D,E,G</K>... (6 Replies)
Discussion started by: rahulmittal87
6 Replies

2. Shell Programming and Scripting

Parse xml file

I am trying to create a shell script that will parse an xml file (file attached). awk '/Id v=/ { print }' Test.xml | sed 's!<Id v=\"\(.*\)\"/>!\1!' > output.txt An output.txt file is created but it is empty. It should contain the value 222159 in it. Thanks. (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

Parse XML File.

HI Guys I have Below XML File : <xn:SubNetwork id="XYZ"> <xn:SubNetwork id="C01"> <xn:MeContext id="CO1"> <xn:ManagedElement id="1"> <un:RncFunction id="1"> <un:UtranCell id="NY431"> ... (2 Replies)
Discussion started by: pareshkp
2 Replies

4. Shell Programming and Scripting

Parse XML

Hi all! I'm looking to write a quick script and in it I need to request an XML file from a service running on localhost and parse that XML file and output it. I'm looking to do it in bash although it doesn't really matter what shell it is in. The XML file returned would look like this: ... (3 Replies)
Discussion started by: mtehonica
3 Replies

5. Shell Programming and Scripting

parse xml file

Hello all, Given the following extract from a xml file with multiple <JOB> .... </JOB> entries <JOB APPLICATION="APP" APR="0" AUG="0" AUTHOR="AUT" AUTOARCH="0" CMDLINE="/tmp/test1 %%var" CONFIRM="1" CREATION_DATE="20100430" CREATION_TIME="130739" ... (2 Replies)
Discussion started by: cabrao
2 Replies

6. Emergency UNIX and Linux Support

How to parse the following xml file

Hi, I have the following file Example.xml <?xml version="1.0" encoding="iso-8859-1"?> <html><set label="09/07/29" value="1241.90"/> </html> Can any one help me in parsing this xml file I want to retrive the attribute values of the tag set Example I want to... (3 Replies)
Discussion started by: Raji_gadam
3 Replies

7. Shell Programming and Scripting

Parse an XML task list to create each task.xml file

I have an task definition listing xml file that contains a list of tasks such as <TASKLIST <TASK definition="Completion date" id="Taskname1" Some other <CODE name="Code12" <Parameter pname="Dog" input="5.6" units="feet" etc /Parameter> <Parameter... (3 Replies)
Discussion started by: MissI
3 Replies

8. Shell Programming and Scripting

How can I parse xml file?

How can I parse file containing xml ? I am sure that its best to use perl - but my perl is not very good - can someone help? Example below contents of file containing the xml - I basically want to parse the file and have each field contained in a variable.. ie. I want to store the account... (14 Replies)
Discussion started by: frustrated1
14 Replies

9. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies

10. Programming

parse xml

Hi, I'm looking for an "easy" way to parse a xml file to a proper structure. The xml looks like this What shall I use? Does anybody has some example-code to share or some good links/book-references? thx for any reply -fe (5 Replies)
Discussion started by: bin-doph
5 Replies
Login or Register to Ask a Question