Bash: How to Filter for Rows where Column is Not Empty


Often you may want to use Bash to filter for the rows in a file where a specific column is not empty.

You can use the following syntax to do so:

awk -F "," '$3 != ""' data1.csv

This particular example will filter the CSV file named data1.csv to only display the rows where the value in column 3 is not empty.

Note that we use the -F operator in awk to specify the delimiter of the file, which is a comma in this example.

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

Example: How to Filter for Rows where Column is Not Empty 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 display the rows where the value in the assists column (column number 3) is not empty.

We can use the following syntax to do so:

awk -F "," '$3 != ""' data1.csv

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

Bash filter for lines where column is not empty

Notice that this filters the CSV file to only display the rows where the value in the third column is not empty.

Note: Feel free to replace $3 with a different number to filter for rows based on a different column number.

Related Tutorials

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

Bash: How to Filter for Lines in File that Start with Specific String
Bash: How to Filter CSV File Based on Column Value
Bash: How to Extract Specific Columns from CSV File

Leave a Reply