2017-04-27 39 views
-1

這或多或少是Using awk to sum the values of a column, based on the values of another column, append the sum and percentage to original data上的變體,但匹配兩個字段而不是一個字段。使用awk對列的值進行求和,根據另一列的值並匹配多個其他列,附加總和和百分比

輸入:

a;x;1 
b;y;2 
a;x;3 

期望的結果:

a;x;1;4;25.00 
b;y;2;2;100.00 
a;x;3;4;75.00 

因此,如果在列1和2是相等的字段,總結它們的列3,並將結果附加。另外,追加百分比。

回答

1

只要改變a[$1]a[$1 OFS $2]我以前answer

$ awk ' 
BEGIN { FS=OFS=";" } 
NR==FNR { s[$1 OFS $2]+=$3; next } 
{ print $0,s[$1 OFS $2],$3/(s[$1 OFS $2]?s[$1 OFS $2]:1)*100 } 
' file file 

未經檢驗的,你。那麼,現在測試。

+0

Humm,沒有工作。給我一秒鐘。啊,只是一個錯字, –

+1

哇,驚人的速度和能力你迴應。非常好,謝謝! – Markus

相關問題