'Sum up numbers each row and column (with awk)?
is there a way to sum up numbers each row and column to each other, maybe by using awk? I created a pivot table, also with awk, and need to add the numbers in a row to the numbers of the row above. It's hard for me to describe, so here is an example:
The current table looks like this: (I need commas as separators)
6,3,8,3,1
8,2,7,7,4
9,9,3,1,0
14,11,5,0,0
The final result should look like this:
6,3,8,3,1
14,5,15,10,5
23,14,18,11,5
37,25,23,11,5
So each number gets added up to the number above. The final table can be a seperate file.
How do I do this?
Solution 1:[1]
Try this:
$ awk 'BEGIN{FS=OFS=","} {for (i=1; i<=NF; i++) { $i = sum[i] += $i; } print}'
6,3,8,3,1
14,5,15,10,5
23,14,18,11,5
37,25,23,11,5
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
