使用awk添加列:
x,y,8,0,0,0,0,0,0,0
m,z,0,0,0,0,0,0,0,0
a,b,206,1,0,2,0,0,0,0
c,d,211,0,0,0,0,0,0,1
e,f,8,0,0,1,0,0,0,0
g,h,5,0,0,0,0,0,0,0
i,j,2,0,0,0,0,0,0,0
k,l,36,0,0,0,0,0,0,0
m,n,2,0,0,0,0,0,0,0
o,p,43,0,0,1,0,0,0,0
q,r,2153,28,1,5,0,0,5,3
s,t,96,0,0,0,0,0,0,0
u,v,25,0,0,0,0,0,0,0
x,y,4,0,0,0,0,0,0,0
,,2799,29,1,9,0,0,5,4
awk -F, '{for (i=1;i<=NR;i++) sum[i]+=$i;}; END{for (i in sum) print sum[i];}' file1
但它在换行符上打印。
答案 0 :(得分:0)
这是sum行并插入每行的最后一行。
awk -F, '{sum=0; for (i=1; i<=NF; i++) { sum+= $i } print $0 "," sum}' file1
要对列进行求和,
awk 'BEGIN{FS=OFS=","} {for (i=1;i<=NF;i++) sum[i]+=$i} END{for(i=1;i<=NF;i++) printf sum[i] OFS; printf "\n"}' file1
测试:
$ awk 'BEGIN{FS=OFS=","} {for (i=1;i<=NF;i++) sum[i]+=$i} END{for(i=1;i<=NF;i++) printf sum[i] OFS; printf "\n"}' file1
0,0,2799,29,1,9,0,0,5,4,
$ awk -F, '{sum=0; for (i=1; i<=NF; i++) { sum+= $i } print $0 "," sum}' test
x,y,8,0,0,0,0,0,0,0,8
m,z,0,0,0,0,0,0,0,0,0
a,b,206,1,0,2,0,0,0,0,209
c,d,211,0,0,0,0,0,0,1,212
e,f,8,0,0,1,0,0,0,0,9
g,h,5,0,0,0,0,0,0,0,5
i,j,2,0,0,0,0,0,0,0,2
k,l,36,0,0,0,0,0,0,0,36
m,n,2,0,0,0,0,0,0,0,2
o,p,43,0,0,1,0,0,0,0,44
q,r,2153,28,1,5,0,0,5,3,2195
s,t,96,0,0,0,0,0,0,0,96
u,v,25,0,0,0,0,0,0,0,25