Pages

Saturday, December 15, 2012

Read/parse XML file using Linux Shell script.

Q. I have a XML file which contain below content, I want to get all IP's in to one array, Passwords in to other array, Usernames in to third array and instanceCount to other array. Can you show me how write a shell script for this?

My XML file content is


<Users>
<Host>
<hostAddress>180.144.226.47</hostAddress>
<userName>pwdfe</userName>
<password>hjitre</password>
<instanceCount>2</instanceCount>
</Host>
<Host>
<hostAddress>180.144.226.87</hostAddress>
<userName>trrrer</userName>
<password>jhjjhhj</password>
<instanceCount>3</instanceCount>
</Host>
<Host>
<hostAddress>180.455.226.87</hostAddress>
<userName>wewqw</userName>
<password>dfsdfd</password>
<instanceCount>3</instanceCount>
</Host>
</Users>



And the file name is temp.xml   Here is the script which will read these values in to different arrays.   

#!/bin/bash j=1
for i in hostAddress userName password instanceCount
do
arrx=($(grep $i temp.xml | sed -r "s/<(|\/)$i>//g"))
eval arr${j}='($(echo ${arrx[@]}))'
((j++))
done
echo "Value of arr1 is ${arr1[@]}"
echo "Value of arr2 is ${arr2[@]}"
echo "Value of arr3 is ${arr3[@]}"
echo "Value of arr4 is ${arr4[@]}"


I used arrays to store the filtered values. Hope this script helps some one.



1 comment:

  1. actually, this is helpful. You just need to consider if the xml have indented lines. Just replace your sed command from:

    sed -r "s/<(|\/)$i>//g"

    to:

    sed -r "s/^[ \t]*//;s/[ \t]*$//;s/<(|\/)$i>//g"

    just to trim trailing and leading tabs/lines.

    Hope this also helps. :)

    ReplyDelete