Quantcast
Channel: Active questions tagged bash+awk - Ask Ubuntu
Viewing all articles
Browse latest Browse all 102

AWK: Sum columns, write them into a "final" row

$
0
0

My CSV looks like this

A   5   3
B   3   1
...

I need to get the sum of all columns and add those to a new line in the CSV so it becomes

A   5   3
B   3   1
SUM 8   4

I was able to print the sum of a particular column by doing awk -F',' '{sum+=$2} END {print sum}' file.csv but I need to do this for a whole folder of CSV's which eventually have to have a sum added to them. Maybe also an empty row between the data set and the "sum" column but that would just be a bonus thing.

I'm a programmer and I could write something like that in Java but I think AWK would get us there way quicker.

Thank you


Viewing all articles
Browse latest Browse all 102

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>