Thursday, February 18, 2010

Read a csv file from unix shell script

The following script can be used to read the contents of a csv file.

The csv file contains the following data

100,TOM,Manager
200,Rachel,CEO

The script will read all the columns by considering the field separator as comma.I've specified the IFS as ','.

/************Start of the scipt **********************/
function readfile {


FILENAME="$1"

exec 8<$FILENAME

while read -u8 line

do

IFS=','

set $line

echo $2

done

}

readfile emp.csv
 
/************End  of the scipt **********************/
 
The function readfile will accept a input parameter file name and it read the second column in the file. the script should be customized according to the requirements
 
 

1 comment:

Anonymous said...

You did an impressive job by posting this. Explained Thoroughly and perfectly.

Unix Training in Chennai | Unix course in Chennai

Powered By Blogger