How to Read a CSV File in Bash

There are primarily two methods to read a CSV file in Bash: using awk and using a while loop with the Internal Field Separator (IFS). This tutorial will not only cover these methods but also show you how to read a specific column from a CSV file.

Sample CSV File

Before diving into the methods, let’s assume that we have a CSV file with the following content:

Name,Age,Occupation
John,30,Engineer
Emily,25,Designer
Michael,35,Manager

Method 1: Using awk

One of the easiest ways to read a CSV file in Bash is by using the awk command-line tool.

Code Example: Reading the Whole File

#!/bin/bash

# Read the entire CSV file using awk
awk -F',' '{print "Name: " $1 ", Age: " $2 ", Occupation: " $3}' your_file.csv

The -F option specifies the field separator that awk should use to split the fields in each line. It’s an abbreviation for “Field Separator.”

In our case, we used -F',' to tell awk that the fields in each line of the CSV file are separated by commas. This is essential for reading the CSV file correctly because awk needs to know how to divide each line into distinct fields or columns.

$1, $2, $3 represent the first, second, and third fields, respectively.

So when awk reads a line like John,30,Engineer, it splits it into three fields:

  • John (accessible as $1 in awk)
  • 30 (accessible as $2)
  • Engineer (accessible as $3)

Code Example: Reading a Specific Column

To read only the “Name” column from the CSV file, you can use the following awk command:

#!/bin/bash

# Read only the "Name" column
awk -F',' '{print $1}' your_file.csv

Method 2: Using while Loop and IFS

Another method is using a while loop along with the Internal Field Separator (IFS).

Code Example: Reading the Whole File

#!/bin/bash

# Read the CSV file line by line
while IFS=',' read -r name age occupation
do
  echo "Name: $name, Age: $age, Occupation: $occupation"
done < your_file.csv

The -r option stands for “raw input.” Adding this option is important as ut prevents read from interpreting any backslashes as escape characters.

Code Example: Reading a Specific Column

To read only the “Age” column, modify the while loop like this:

#!/bin/bash

# Read only the "Age" column
while IFS=',' read -r name age occupation
do
  echo $age
done < your_file.csv

Conclusion

By now, you should have a good understanding of the two primary methods for reading CSV files in Bash. We’ve also covered how to read specific columns, which is especially useful when dealing with large files or when you only need particular data.