Bash Read Comma Separated CVS File

by on August 14, 2006 · 8 comments· LAST UPDATED August 14, 2009

in , ,

How do I read comma separated CVS file under UNIX / Linux / BSD / Mac OS X bash script? My sample file is as follows:

FirstName LastName,DOB,SSN,Telephone,Status


You can use while shell loop to read comma-separated cvs file. IFS variable will set cvs separated to , (comma). read command will read each line and store data into each field.

#!/bin/bash
INPUT=data.cvs
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read flname dob ssn tel status
do
	echo "Name : $flname"
	echo "DOB : $dob"
	echo "SSN : $ssn"
	echo "Telephone : $tel"
	echo "Status : $status"
done < $INPUT
IFS=$OLDIFS
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 8 comments… read them below or add one }

1 alexey February 8, 2011 at 9:52 pm

script that worked for me

#!/bin/bash
INPUT=./Zabbix_template_item.csv
OLDIFS=$IFS
IFS=";"
[ ! -f $INPUT ] &while read Host Group itItemType itKey itValueType itDescription itDelay itHistory itTrends itDataType itUnits itMultiplier itDelta itFormula itParams itSnmpCom itSnmpOid itApplication
do
        echo "Host : $Host"
        echo "Group : $Group"
        echo "itItemType : $itItemType"
        echo "itKey : $itKey"
        echo "itValueType : $itValueType"
        echo "itDescription : $itDescription"
        echo "itDelay : $itDelay"
        echo "itHistory : $itHistory"
        echo "itTrends : $itTrends"
        echo "itDataType : $itDataType"
        echo "itUnits : $itUnits"
        echo "itMultiplier : $itMultiplier"
        echo "itDelta : $itDelta"
        echo "itFormula : $itFormula"
        echo "itParams : $itParams"
        echo "itSnmpCom : $itSnmpCom"
        echo "itSnmpOid : $itSnmpOid"
        echo "itApplication : $itApplication"
done < $INPUT
IFS=$OLDIFS

Reply

2 Shazaad September 18, 2011 at 7:30 am

How to skip commented/blank lines in the CSV file?

Reply

3 roop February 7, 2014 at 1:58 pm

i have used the same code to read my csv file but i cant read the last row of my csv file using while loop. please help.

Reply

4 sunil May 17, 2012 at 7:46 am

I have two Csv files one is for daily report purposed and another is for daily data purposed.
masterreport.csv file format is

CODE,1-May-12,2-May-12,3-May-12,4-May-12,5-May-12,
205,0,0,0,0,0,
23070,0,0,0,0,0,
Interface error response CRBT fail,0,1,0,0,0,
C2S exception,0,1,2,0,2,
Interface Customer Recharge Not Allowed For Receiver,2240,2078,2050,2007,2363,

—–Many more up to 45 Rows and 32 column are there
and every day we have to update the corresponding date part from below mention file.

dailyreport.csv
C2S ERROR EXCEPTION TAKING TIME TILL VALIDATION , 624
CHNL_ERROR_SNDR_AMT_NOTBETWEEN_MINMAX , 56
Card Group Slab Suspended , 7096

like how many error’s are coming on that day we have to update the main report , which are not match daily report we put the value 0 on main report.

i didn’t found any logic how to do this , can any body help this

Reply

5 bibliotranstornado May 31, 2012 at 12:31 pm

15 years working with csv files in bash and I didn’t know this method!

Thanks a lot.

Reply

6 Dushyant October 20, 2012 at 12:37 pm

wow it’s really helpful.

Thx.

Reply

7 lyonn July 15, 2013 at 10:36 pm

This way to get fields into a CSV is easy to use.

Reply

8 Damir September 10, 2013 at 9:34 am

Exactly what i needed.
Thanx

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: