Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nepsilon/37beee8d266bfd6d33707fec79edb4b9 to your computer and use it in GitHub Desktop.
Save nepsilon/37beee8d266bfd6d33707fec79edb4b9 to your computer and use it in GitHub Desktop.
In a CSV file, how to sum up all numbers in a given column? — First published in fullweb.io issue #87

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 colum for each line, and print it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment