≡ Menu

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.

[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read flname dob ssn tel status
	echo "Name : $flname"
	echo "DOB : $dob"
	echo "SSN : $ssn"
	echo "Telephone : $tel"
	echo "Status : $status"
done < $INPUT
Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 13 comments… add one }
  • alexey February 8, 2011, 9:52 pm

    script that worked for me

    [ ! -f $INPUT ] &while read Host Group itItemType itKey itValueType itDescription itDelay itHistory itTrends itDataType itUnits itMultiplier itDelta itFormula itParams itSnmpCom itSnmpOid itApplication
            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
    • Shazaad September 18, 2011, 7:30 am

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

    • roop February 7, 2014, 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.

      • Jake October 22, 2014, 5:06 pm

        I’m having the same issue. The last row of my csv isn’t being read. Did you find a solution roop?

        • srikanth May 28, 2016, 1:22 pm

          I am having the same issue. will try to figure out ans post it.

  • sunil May 17, 2012, 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

    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.

    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

  • bibliotranstornado May 31, 2012, 12:31 pm

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

    Thanks a lot.

  • Dushyant October 20, 2012, 12:37 pm

    wow it’s really helpful.


  • lyonn July 15, 2013, 10:36 pm

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

  • Damir September 10, 2013, 9:34 am

    Exactly what i needed.

  • Isaac June 4, 2015, 3:51 pm

    This was exactly what I needed! I’m using it to import account data from a Zimbra server backup.

    IE account names are stored in the CSV and the script runs the import command with the $flname variable in the appropriate spots.

    I do have a question, How does it know to hit the next line and not just read the first line every time? My assumption is that is what the $IFS & $OLDIFS variables do. But I’m not sure.

  • znik70 June 28, 2016, 7:45 am

    This method is only for regular simplest version of CSV. very often fields are in quotation marks and it contains comma. In that situation for row
    read c1 c2 c3 assign

    then this method is not as universal as it should be.

    last problem, very often last row in csv file is not ended with new line. in that situation read has some problem with fetching last row.

    summarizing. this is the simplest way for reading the simplest cvs formatting.

  • ben October 29, 2016, 8:38 pm

    Awk solution on github: https://github.com/benalt613/csv

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , , , , , , , , , ,