Getting variables out of .csv lines

I have a csv file looking like:

echo,w-cai,w-cai-ssl
echo,countrywide,countrywide-ssl
haystack,intranet3,intranet3-ssl
haystack,pnf,pnf-ssl

Basically, I want to process this file row by row assigning each word delimited by a comma to a variable.

ie. for the first row, $variable1=echo $variable2=w-cai and $variable3=w-cai-ssl

these variables are used and then the next row is read with the variables now taking the values of those on row 2, delimited by commas.

Any script suggetsions ti do this?

You could do one of the following:
Use awk to process your file by setting the -F (field separator) value to a comma
or
you could use sed to change the commas to spaces and then process each line as if it were a list (assuming your list in not extraordinarily long
or
you could again use sed to change the commas to spaces, then read each line into an array then process the file.

You could also just change the IFS variable to ',' and the use 'read' to read each value. The 'cut' command could be of some use here if you need to pull individual fields, but "awk -F\," would probably give you a bit more power to do what you need.

Somthing like this may work for you:

#! /usr/bin/ksh
FILE=somefile.txt
cat ${FILE} | sed 's/,/ /g' | while read LINE
do
set ${LINE}
< do what you want to do here>
done

A little explanation. I think you can follow the cat and sed command. The 'while read LINE' basically reads one line at a time and pumps it into the body of the while loop. The 'set ${LINE}' is the cool part. It sets the first string up to the first white space to $1. It sets the second string to the second white space at $2, and so forth. You can then use these variables to do what ever you need to do. This is much more effiecient then doing a for or while loop and then awking each variable individually.

thanks for all the replys guys, I opted for the 'IFS' method which was recommended by someone I work with, and which seems to be perfectly suited for processing csv files.

Thanks again for all the help, there really seems to be no unix problem that can't be posted and solved in this forum! It really is the best around. :slight_smile:

oh, just to clarify, i used the 'set' command to parse the line into data elements (as tony suggested) which I thought was quite cool! (i'm easily amused... :stuck_out_tongue: )