shell - how to sum each column in a file using bash -
i have file on next format
id_1,1,0,2,3,lable1 id_2,3,2,2,1,lable1 id_3,5,1,7,6,lable1
and want addition of each column ( have on 300 columns)
9,3,11,10,lable1
how can using bash. tried using described here didn't work.
if totals need grouped label in lastly column, seek this:
awk -f, ' { l[$nf] for(i=2; i<nf; i++) t[$nf,i]+=$i } end{ for(i in l){ s=i for(j=nf-1; j>1; j--) s=t[i,j] fs s print s } } ' file
if labels in lastly column sorted seek without arrays , save memory:
awk -f, ' function labelsum(){ s=p for(i=nf-1; i>1; i--) s=t[i] fs s print s split(x,t) } p!=$nf{ if(p) labelsum() p=$nf } { for(i=2; i<nf; i++) t[i]+=$i } end { labelsum() } ' file
bash shell awk
No comments:
Post a Comment