user3871995
user3871995

Reputation: 1233

Get the sum of a particular column using bash?

I am trying to get the sum of the 5th column of a .csv file using bash, however the command I am using keeps getting me zero. I am piping the file through a grep to remove the column header row:

grep -v Header results.csv | awk '{sum += $5} END {print sum}' 

Upvotes: 0

Views: 4033

Answers (1)

webb
webb

Reputation: 4340

here's how I would do it:

tail -n+2 | cut -d, -f5 | awk '{sum+=$1} END {print sum}'

or:

tail -n+2 | awk -F, '{sum+=$5} END {print sum}'

(depending on what turns out to be faster.)

Upvotes: 3

Related Questions