Parse variable length status,timestamp CSV

I have the output of a process which on status change of the object being processed appends status,timestamp to the record in the text file...

so i end up with output something like:

 
task123,TERMINAL,glob,5,INITIAL,2012-02-27 16:48:07,PREPARING,2012-02-27 16:49:06,SCHEDULED,2012-02-27 16:49:06,TRANSLATING,2012-02-27 16:49:07,TERMINAL,2012-02-27 16:50:52
task345,COMPLETE,glob2,6,INITIAL,2012-02-27 16:48:07,PREPARING,2012-02-27 16:49:06,SCHEDULED,2012-02-27 16:49:07,TRANSLATING,2012-02-27 16:49:22,LOADING,2012-02-27 16:50:53,COMPLETE,2012-02-27 16:50:53

what i'd like to do is parse this into a set of variables for a given job (i'll end up looping through the file record by record)...

what i'm not sure is how to get the value after a matched string and save it in a variable...

so for example i want to save the timestamp following "PREPARING" into a variable called v_PREPARING

and i want to iterate through the line to get all of these values... the good thing i guess (though not sure how its helpful) is that the value after glob/glob2 is a count of the number of status' on that line...

thoughts?

Try this:

while IFS=, read job status glob cnt rest
do
    eval $(echo $rest | awk -F, '{for(i=1;i<NF;i+=2)print "v_"$i"=\""$(i+1)"\""}')
 
    # Your code goes here....
    echo "Task: " $job
    echo "Status: " $status
    echo "INITIAL: " $v_INITIAL
 
    #Unset all vars for next loop around
    eval $(echo $rest | awk -F, '{for(i=1;i<NF;i+=2)print "unset v_"$i}')
done < infile

I'd avoid the evals if I could...

#!/bin/bash

IFS=","
while read LINE
do
        set -- $LINE

        TERMINAL=
        INITIAL=
        PREPARING=
        SCHEDULED=
        TRANSLATING=

        TASK=$1
        shift        

        while [ "$#" -gt 0]
        do
                VAR=$1
                shift
                read $VAR <<<"$1"
        done
done