我有这个脚本:
#!/usr/local/bin/gawk -f
BEGIN{
FS="=|,"
PROCINFO["sorted_in"]="@ind_num_asc";
printf "\n"
printf "%-7s %s", "Count", "Amount"
printf "\n"
OFS="\t"
}
/phrase/{
for (i=4; i 3<= 5; i++ )
if ($i != "") a[$i]++
}
END{
for (i in a) {print a[i], i; tot++}
printf "\n"
printf " ***** %s total wins *****", tot
printf "\n"
}
产生此输出:
Count Amount
1 20
1 22
1 29
1 37
1 38
1 45
1 46
2 80
1 99
1 800
***** 10 total unique amounts *****
我想打印出第二个字段的总和,但请注意计数数量从1到很多不等。 for循环中是否应该有一个while循环来总结计数或在END的脚本结尾处进行数学计算?
感谢您提供的任何提示!
gawk用
提取的示例数据gawk -F"=|," '/phrase/ {print $4}' file
80
800
20
46
38
45
99
80
29
22
37
解析前的数据示例:
.\phrase(100): [LOG] API context: context=3, amount=80
.\phrase(100): [LOG] API context: context=3, amount=800
.\phrase(100): [LOG] API context: context=3, amount=20
.\phrase(100): [LOG] API context: context=3, amount=46
.\phrase(100): [LOG] API context: context=3, amount=38
.\phrase(100): [LOG] API context: context=3, amount=45
.\phrase(100): [LOG] API context: context=3, amount=99
.\phrase(100): [LOG] API context: context=3, amount=80
.\phrase(100): [LOG] API context: context=3, amount=29
.\phrase(100): [LOG] API context: context=3, amount=22
.\phrase(100): [LOG] API context: context=3, amount=37
预期结果:
Count Amount
1 20
1 22
1 29
1 37
1 38
1 45
1 46
2 80
1 99
1 800
***** 10 total unique amounts *****
***** 1296 sum totals *****
答案 0 :(得分:2)
关注awk
可能对您有帮助。
awk -F'=' '{a[$NF]++;sum+=$NF} END{print "Count Amount";for(i in a){print a[i]"\t"i;};print "***** " length(a),"total unique amounts *****" RS "***** " sum "sum totals *****"}' Input_file
输出如下。
Count Amount
2 80
1 45
1 37
1 46
1 29
1 38
1 20
1 22
1 800
1 99
***** 10 total unique amounts *****
***** 1296sum totals *****
答案 1 :(得分:2)
基本上它应该是:
awk -F= '{a[$NF]++;t+=$NF} # You can calculate the total here
END{
for(i in a) print a[i], i
printf "%s uniq\n", length(a)
printf "%s total\n", t
}' a.txt
为简洁起见,我省略了排序和打印标题。