使用awk计算一列中单词出现的次数 [英] Using awk to count the number of occurrences of a word in a column
本文介绍了使用awk计算一列中单词出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
03/03/2014 12:31:21 BLOCK 10.1.34.1 11:22:33:44:55:66
03/03/2014 12:31:22 ALLOW 10.1.34.2 AA:BB:CC:DD:EE:FF
03/03/2014 12:31:25 BLOCK 10.1.34.1 55:66:77:88:99:AA
我正在尝试使用awk来在一个命令中计算上面单词"block"和"access"的出现次数.
I am trying to use awk to count the number of occurrences of the word "block" and "access" above in one command.
我最初尝试使用阻止"一词,但我的计数器似乎无法正常工作.谁能看到我的代码在哪里错误?
I tried the word "block" at first but my counter does not appear to be working. Can anyone see where my code is wrong?
awk ' BEGIN {count=0;} { if ($3 == "BLOCK") count+=1} end {print $count}' firewall.log
推荐答案
使用数组
awk '{count[$3]++} END {for (word in count) print word, count[word]}' file
如果要专门阻止":END {print count["BLOCK"]}
这篇关于使用awk计算一列中单词出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文