使用awk对一列的值求和,基于另一列的值并匹配其他多个列,追加总和和百分比

Using awk to sum the values of a column, based on the values of another column and matching multiple other columns, append the sum and percentage

这或多或少是 的变体,但匹配两个字段而不是一个。

输入:

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 列求和并附加结果。另外,附加百分比。

只需将我之前中的a[]改为a[ OFS ]即可:

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

未经测试,你。嗯,现在测试了。