Shell Script to read XML tags and the data within that tag

Hi unix Gurus,
I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information.
For example

<SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME ="ACTRL_BNFT_KEY_DMN" OBJECTVERSION ="1" OWNERNAME ="COC_V20_ETL_APPL" VERSIONNUMBER ="1">
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" DESCRIPTION ="" FIELDNUMBER ="1" FIELDPROPERTY ="0" FIELDTYPE ="ELEMITEM" HIDDEN ="NO" </SOURCE>

From the above xml file , I have to read the xml file and get the source name from the file as the below output.

SOURCE ->ACTRL_BNFT_KEY_DMN

.

Please help me UNIX Gurus.

A bash script for extracting SOURCE NAME:

#!/bin/bash

F=0
while read line
do
        [[ ! "$line" =~ ^\<SOURCE[[:space:]] ]] && continue

        for attr in $line
        do
                if [[ "$attr" =~ ^NAME$ ]]
                then
                        F=1
                        continue
                fi
                if [ $F -eq 1 ]
                then
                        attr="${attr%\"*}"
                        printf "SOURCE -> %s\n" "${attr#*\"}"
                        F=0
                fi
        done

done < file.xml

Using awk

awk '$1=="<SOURCE"{for(i=1;i<=NF;i++){if($i=="NAME"){gsub(/=|"/,x,$(i+1)); print "SOURCE -> " $(i+1)}}}' file.xml
1 Like

Thanks Yoda,

When i tried to execute the script with my xml, the script is fetching some more values like "$$ infromation"

instead of using Name as delimiter. Can i use "_SRC" so that the script search for the particular word which ends with _SRC.

Thanks Again