Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Jul 292013
 

How to sum a particular column of a CSV file with awk

Answer:

Assume you have a CSV file like the following (only contain number)

1,2,3
2,3,4
3,4,5
...

How do you sum the nth column of this file? It is easy with the use of awk

e.g. Assume you want to sum the 2nd column:

# awk -F "," '{ sum += $2 } END { print sum }' test.txt

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>