Bash: How to Read Columns from CSV File into Arrays


Often you may want to use Bash to read the columns from a CSV file into individual arrays.

You can use the following syntax to do so:

array1=( $(tail -n +2 nba_data.csv | cut -d ',' -f1) )
array2=( $(tail -n +2 nba_data.csv | cut -d ',' -f2) )
array3=( $(tail -n +2 nba_data.csv | cut -d ',' -f3) )

echo "array of teams: ${array1[@]}"
echo "array of points: ${array2[@]}"
echo "array of assists: ${array3[@]}"

This particular example reads three columns from the CSV file named nba_data.csv and places the values from each column into their own array.

A couple notes on the syntax:

  • We use tail -n +2 to specify that we want to read all lines in the file starting from line 2. This allows us to exclude the values from the header row.
  • We use cut to specify that we’d like to filter the value in each row using commas as the -d delimiter and -f to specify the field (column) to select the value from.

The following example shows how to use this syntax in practice.

Related: How to Read CSV File and Skip First Line in Bash

Example: How to Read Columns from CSV File into Arrays in Bash

Suppose that we have a file named nba_data.csv.

We can use the cat statement to view the contents of this file:

This file contains information about the team, points scored and total assists for various basketball players.

Notice that the first row is simply a header row that contains the values team, points and assists.

Suppose that we would like to read the values from each of the three columns into their own arrays.

We can use the following syntax to do so:

array1=( $(tail -n +2 nba_data.csv | cut -d ',' -f1) )
array2=( $(tail -n +2 nba_data.csv | cut -d ',' -f2) )
array3=( $(tail -n +2 nba_data.csv | cut -d ',' -f3) )

echo "array of teams: ${array1[@]}"
echo "array of points: ${array2[@]}"
echo "array of assists: ${array3[@]}"

Each of the three columns will now be placed into an array.

We can use the echo statement to retrieve the values from a given array.

For example, we can use the following syntax to retrieve the array of team  names:

echo "array of teams: ${array1[@]}"

The following screenshot shows how to use this syntax in practice:

Bash read columns of CSV file into arrays

Notice that this returns an array of only the values from the first column (the “team” column) without the header row.

We can use similar syntax to retrieve the array of points and the array of assists.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Replace Special Characters in String
Bash: How to Replace Specific Line in File
Bash: How to Replace All Occurrences of String in File

Leave a Reply