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