I have a file on the following format
id_1,1,0,2,3,lable1
id_2,3,2,2,1,lable1
id_3,5,1,7,6,lable1
and I want the summation of each column ( I have over 300 columns)
9,3,11,10,lable1
how can I do that using bash. I tried using what described here but didn't work.
Here's a Perl one-liner:
It will only do sums, so the first and last column in your example will be 0.
This version will follow your example output:
Pure bash solution:
Using
awk
:This will print the sum of each column (from i=2 .. i=n-1) in a comma separated file followed the value of the last column from the last row (i.e. lable1).
If the totals would need to be grouped by the label in the last column, you could try this:
If the labels in the last column are sorted then you could try without arrays and save memory:
A modified version based on the solution you linked: