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

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

2 Answers

You could use something like this. It works for files with an arbitrary number of columns, assuming that the first column is text and shall have SUM in the result line instead of the sum of all values of that column.

$ awk '{for(i=2;i<=NF;i++)a[i]+=$i;print $0} END{l="SUM";i=2;while(i in a){l=l" "a[i];i++};print l}' data.csv > final.csv
A 5 3
B 3 1
SUM 8 4

The awk code formatted in a more readable way:

{ for (i=2 ; i<=NF ; i++) a[i] += $i print $0
}
END { l = "SUM" i=2 while(i in a) { l = l " " a[i]
  i++ } print l
}
7

Found something that at first didn't work for me on StackOverflow

awk -F',' '{ print($0); len=split($0,a); if (maxlen < len) { maxlen=len; } for (i=1;i<=len;i++) { b[i]+=a[i]; }
}
END { for (i=1;i<=maxlen;i++) { printf("%s,", b[i]); } print ""
}' data.csv >> final.csv

It for some reason adds another column to my data but that's ok, I can work with that.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like