Parsing a CSV file using Bash

Apr 13, 2020 min read

Often times I need to do something multiple times with different input values. Like recently I needed to add a bunch of folks to a team in GitHub for an upcoming training session, easy to do for one or two, not for 200+. Luckily I had everyone’s information in a .csv file, that turned into a quick bash script, easy peezy.

The command to loop through a .csv file is pretty simple, it looks like:

parse-data.sh

#!/bin/bash

FILE = $1

while IFS="," read f1 f2
do
  # Do something with that value, like a cURL command
  echo "First name is $f1"
  echo "Last name is $f2"
done < $FILE

Here I’m using the Internal Field Separator (IFS) to separate each line by the “,” value. For each field I’m then representing it’s value with the f1 f2... values. Note: each field in the .csv should be represented with a variable, this will cause issues if your .csv file contains more fields than are read into the while loop.

Example data:

data.csv

First_Name, Last_Name
James,Dean
Spider,Man
...

To use:

./parse-data.sh data.csv

Result:

First name is James
Last name is Dean
First name is Spider
Last name is Man