我有以下数据;
24692 -rw-rw-r--+ 1 da01 da01 25284427 Aug 31 09:06 collected_BOT.227031
24660 -rw-rw-r--+ 1 da01 da01 25248756 Aug 31 09:35 collected_BOT.227032
24748 -rw-rw-r--+ 1 da01 da01 25338868 Aug 31 10:03 collected_BOT.227033
24740 -rw-rw-r--+ 1 da01 da01 25331322 Aug 31 10:31 collected_BOT.227034
样品:
grep 1303 collected_BOT.227034 | more
1559254293,151840703,AJ1X,10178801756650692,VA,VB,0,0,2,2,1303,1,L1O,6797,129,1,3,601,0,GVW1,9110,551,17,000000,0001,000000,,6,4,,1,1,,0
1559254294,151840704,AJ2X,10178801756650693,VA,VB,0,0,2,2,1303,1,L2O,6797,203,1,3,601,0,GVW2,9110,552,17,000000,0001,000000,,6,4,,1,1,,0
1559254295,151840705,AJ3X,10178801756650694,VA,VB,0,0,2,2,1303,1,L3O,6797,664,1,3,601,0,GVW3,9110,552,17,000000,0001,000000,,6,4,,1,1,,0
$15 = duration
我只想计算档案$15
上的总金额collected_BOT.227034
(仅当 $ 11 = 1303 时)
答案 0 :(得分:1)
awk -F, '$11==1303{sum+=$15} END {print sum}' collected_BOT.227034
-F,
字段分隔符为,
$11==1303
检查11th
字段是否与数字1303
完全匹配
15th
字段的值添加到sum
变量(默认情况下其初始值为零)END {print sum}
,打印sum
变量的值修改强>
感谢@Mark Setchell指出可以使用$11==1303
代替$11 ~ /^1303$/
此外,如果输出需要为'0',即使没有行匹配,也请使用print sum + 0
。或明确的BEGIN{sum=0}
块
答案 1 :(得分:0)
很好的解决方案@sp asic。
不需要为字段$11
使用正则表达式:
awk -F, '$11=="1303" {sum+=$15} END {print sum}' collected_BOT.227034
(注意:使用==
而不是=
,因为除了(成功)分配到字段$11