Bash Read Comma Separated CVS File
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 |