In a CSV file, how to sum up all numbers in a given column?

When you only have a few thousand lines, a spreadsheet software will do. But when you got millions, it’s another job.

Unix has the awk command, which you might not use too often, if at all, but is both powerful and easy get started with. See here how to sump up all numbers in the 3rd column in records.csv:

awk -F',' '{sum+=$3} END {print sum}' records.csv

This boils down to “repeat instructions on each line”, then execute instructions after the END keyword. And segment the line on the , character. Each line segment is then referenced with the $ sign, the first is $1, etc.

Here we simply initialize and assign/add the value in the 3rd column for each line, and print it.

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.