awk 按列分组并对多个值求和

awk 按列分组并对多个值求和

我正在尝试按“名称”和“数量”列和“价格”列进行分组,示例数据如下:

names,fruits,qty,price
tom,banana,2,500
tom,banana,3,750
tom,apple,2,500
alex,banana,3,750
alex,melon,3,750
alex,melon,3,750
jess,banana,1,250
jess,banana,1,250
jess,banana,1,250
danny,melon,2,500
danny,apple,2,500
danny,apple,2,500

我已经尝试使用这个命令:

awk -F, 'BEGIN{FS=OFS=","} 
            NR==1{print; next} 
            {
                q=$3; 
                $3="~";
                w=$4; 
                $4="~";
                a[$0]+=q;
                b[$0]+=w
            } 
       END  {
                for(k in a) 
                {
                    sub("~",a[k],k); 
                    sub("~",b[k],k);
                    print k
                }
            }' file

使用该命令,我在“价格”列中变为空,我想要的输出如下所示:

names,fruits,qty,price
alex,banana,3,750
tom,banana,5,1250
alex,melon,6,1500
jess,banana,3,750
danny,apple,4,1000
danny,melon,2,500
tom,apple,2,500

先谢谢您的帮助。

答案1

你可以做:

awk -F, -v OFS=, '
 NR==1{ print; next }
 { key=($1 OFS $2) }
 { grpByQty[key]+=$3; grpByPrice[key]+=$4 }
END{ for(key in grpByQty) print key, grpByQty[key], grpByPrice[key] }' infile

答案2

BEGIN {
    FS = OFS = ","
}

NR == 1 {
    print $0
    next
}

NR > 1 {
    ori = price[$1 "," $2]
    if (ori == "") {
        price[$1 "," $2] = $3 "," $4
    } else {
        split(ori, a, ",")
        price[$1 "," $2] = a[1] + $3 "," a[2] + $4
    }
}

END {
    for (name in price) {
        print name, price[name]
    }
}

name这个想法是将与“,”连接的 和fruit作为键存储,quantity与“,”连接的 和price作为值。

相关内容