Bash: How to Filter CSV File Based on Column Value


Often you may want to use Bash to filter the rows of a CSV file based on the value in a particular column.

You can use the following methods to do so:

Method 1: Filter CSV File Where Column Equals String

awk -F "," '$1 == "Mavs"' data1.csv > data2.csv

This particular example will filter the CSV file named data1.csv to only contain rows where the value in column 1 is equal to “Mavs” and then output these filtered rows to a CSV file named data2.csv.

Method 2: Filter CSV File Where Column Meets Condition

awk -F "," '$2 > 20' data1.csv > data2.csv

This particular example will filter the CSV file named data1.csv to only contain rows where the value in column 2 is greater than 20 and then output these filtered rows to a CSV file named data2.csv.

The following examples show how to use each method in practice.

Example 1: Filter CSV File Where Column Equals String in Bash

Suppose that we have a CSV file named data1.csv that contains information about various basketball players.

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

Suppose that we would like to filter the file to only contain rows where the value in the first column is equal to “Mavs” and output these filtered rows to a new file.

We can use the following syntax to do so:

awk -F "," '$1 == "Mavs"' data1.csv > data2.csv

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

Bash filter CSV based on column value

When we use the cat command to view the contents of the data2.csv file, we can see that it only contains rows where the value in the first column of the file is equal to “Mavs.”

Example 2: Filter CSV File Where Column Meets Condition in Bash

Suppose that we would like to filter the data1.csv file to only contain rows where the value in the second column is greater than 20 and output these filtered rows to a new file.

We can use the following syntax to do so:

awk -F "," '$2 > 20' data1.csv > data2.csv

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

Bash filter CSV file where column meets condition

When we use the cat command to view the contents of the data2.csv file, we can see that it only contains rows where the value in the second column is greater than 20.

Related Tutorials

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

Bash: How to Read Columns from CSV File into Arrays
Bash: How to Read CSV File and Skip First Line
Bash: How to Extract Specific Columns from CSV File

Leave a Reply