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:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 1 comment… read it below or add one }
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