In a CSV file, how to count all lines matching a condition?

Following on our CSV file processing series, here is how to count the number of lines that match a condition.

We’ll keep using awk. Here is the command to run on the records.csv file:

awk -F, '{if ($5 == "foo") count+=1} END {print count}' records.csv

Easy enough, -F, tells each line use the comma to delimit each column. Then for each line in the file, check if the 5th column is equal to foo. If true then increment the count variable by 1.

When done processing all lines, just print count.

Voila, you’re ready to process GB of CSV files without killing your spreadsheet software or writing a single line of code!

Like what you read? Give Julien Buty a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.