Using awk to count the number of occurrences of a word in a column

后端 未结 6 1791
难免孤独
难免孤独 2020-12-03 10:28
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
<         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-03 11:19

    The reason is that you just need to print count rather than $count. Inside awk, you do not need to use $ to find variable. In your case, the awk will try to print $2 before ending which does not exit. Below code should work:

    awk ' BEGIN {count=0;} { if ($3 == "BLOCK") count+=1} END {print count}' firewall.log

提交回复
热议问题