Bash Read Comma Separated CVS File

by Vivek Gite on August 14, 2006 · 1 comment

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 1 comment… read it below or add one }

1 alexey February 8, 2011

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

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 9 + 14 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: