Trying to pull a variable out of an xml file...

Hello. I'm new to *ix and am trying to pull a variable or two from an xml document. The document is in the format:

<name>7_3(A).mov</name>
      <description>Some description from a file</description>
      <updatename>7_3_A.mov</updatename>
      <updatepath>Dailies Released</updatepath>
      <playlist>Selects Day 5,Dailies Day 5</playlist>

What I need to do is take the value or values from inside the <playlist> tags and assign them to variables. So, for this example, I'd like to be able to set $playlist1 to "Selects Day 5" and $playlist2 to "Dailies Day 5." The values in that xml tag will always be comma separated and there can be any number of them. I used grep to find the playlist lines, but don't know what to do next.

There are many more lines for each item described in the xml and there will be other instances of the <playlist> tag. I'll need to ignore instances of any value already set so, for example, once I've got the values "Selects Day 5" and "Dailies Day 5" from the above example I will need to ignore any further instances of those values until the script is executed again.

Any help would really be appreciated.

Thanks!

Dan

This will parse your document and populate a shell array, provided the number of unique "names" isn't larger than the shell will support. It does make the assumption that <playlist> and </playlist> tags do not have a newline between them.

IFS=, playlists=( $(awk '
    /<playlist>/ {
        gsub( "</playlist>.*", "", $0 );   # strip everything before and after
        gsub( ".*<playlist>", "", $0 );
        n = split( $0, a, "," );             # split each name and make unique
        for( i = 1; i <= n; i++ )
            seen[a] = 1;
    }

    END {
        for( x in seen )               # print the list of unique "names"
            printf( "%s,", x );
        printf( "\n" );
    }
' file.xml  ) )

You can access the list with [icode]${playlists[$i]}[/code], here is a sample loop that just prints them out:

i=0
while (( $i < ${#playlists[@]} ))
do
    echo "$i ${playlists[$i]}"
    i=$(( i + 1 ))
done

If you don't want to collect them in an array, you can read the awk output in a while loop:

awl '... above programme ' file.xml | while read name
do
   echo "$name"  # or other real work
done
 
result=$(nawk -F"[<>,]" '/playlist/ {printf("%s,%s\n",$3,$4)}' input.xml)
var1=$(echo $result | cut -d, -f1)
var2=$(echo $result | cut -d, -f2)

Thank you worked perfectly!