【问题标题】: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使用awk对一列的值求和,根据另一列的值并匹配多个其他列,附加总和和百分比
【发布时间】:2017-04-27 20:03:07
【问题描述】:

这或多或少是 Using awk to sum the values of a column, based on the values of another column, append the sum and percentage to original data 的变体,但匹配两个字段而不是一个。

输入:

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

【问题讨论】:

    标签: awk sum append percentage


    【解决方案1】:

    只需将我之前的answer 中的a[$1] 更改为a[$1 OFS $2]

    $ 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
    

    未经测试,你。好了,现在测试一下。

    【讨论】:

    • 哇,您的反应速度和能力令人惊叹。效果很好,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多