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


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
arrx=($(grep $i temp.xml | sed -r "s/<(|\/)$i>//g"))
eval arr${j}='($(echo ${arrx[@]}))'
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"


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

    just to trim trailing and leading tabs/lines.

    Hope this also helps. :)