如何基于Awk中另一列的值求和列中的值

时间:2019-10-12 13:36:27

标签: awk

输入:

TT,SS,ECID,CDID,ODID,Symbol,Side,LastQty,LastPx,CumQty,AvgPx, 
"20191008-13:32:52","RO","0284","378MT","r7ot","SPD","1","100","290.67","400","290.67", 
"20191008-13:33:13","RO","02DJ","378MT","r7o","SPD","1","100","290.68","2248","290.655", 
"20191008-13:33:26","RO","FATS","378MTA","r7ot","PDF","1","100","290.92","2751","290.608",

输出应为:

SPD 200
PDF 100

试图用它来做,但不起作用

$ awk '{a[$3]+=$4}END{for(i in a) print i,a[i]}' file

1 个答案:

答案 0 :(得分:2)

EDIT2: :由于OP具有旧的awk,其中FPAT不存在,因此根据示例,我随后添加了以下代码。

awk -F, '{gsub(/\r/,"")} FNR>1{gsub(/"/,"",$8);gsub(/"/,"",$6);a[$6]+=$8} END{for(i in a){print i,a[i] | "sort -k1"}}' Input_file


编辑: :由于OP完全更改了Input_file,因此现在添加此解决方案。用GNU awk编写和测试。

awk -v FPAT='[^,]*|"[^"]+"' '
gsub(/\r/,"")
FNR>1{
  gsub(/"/,"",$8)
  gsub(/"/,"",$6)
  a[$6]+=$8
}
END{
  for(i in a){
    print i,a[i]
  }
}
'   Input_file

或按字母顺序对输出进行排序,请尝试以下操作。

awk -v FPAT='[^,]*|"[^"]+"' '{gsub(/\r/,"")} FNR>1{gsub(/"/,"",$8);gsub(/"/,"",$6);a[$6]+=$8} END{for(i in a){print i,a[i] | "sort -k1"}}'  Input_file


您亲近了,您的方法遇到的问题是您尚未在代码中将字段分隔符设置为,,但是您的Input_file的分隔符为,,因此它的位置没有$3所有,因此无法正常工作。您能不能试一下。

awk -F"[[:space:]]*,[[:space:]]*" 'FNR>1{a[$3]+=$4} END{for(i in a){print i,a[i]}}' Input_file

PS:感谢oguz ismail通知了字段分隔符集。