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

后端 未结 6 1783
难免孤独
难免孤独 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:23

    The reason that your code may not be working is END is case sensitive so your script will be checking the variable end exists(which it doesn't) and so the last block will never be executed. If you change that then it should work.

    Also you do not need the BEGIN block as all variable are instantiated at 0.

    Below I have added an alternative way of doing this that you may want to use instead.

    This is similar to glenn's but captures only the words you want, it should use little memory because of this.


    Using Gawk(for the third arg of match)

    awk 'match($3,/BLOCK|ALLOW/,b){a[b[0]]++}END{for(i in a)print i ,a[i]}' file
    

    This block only executes if BLOCK or ALLOW are contained in the third field.
    The match captures what has been matched into the array b.
    Then array a is incremented for the matched field.

    In the END block each captured field is outputted with a count of occurences.


    The output is

    ALLOW 1
    BLOCK 2
    

提交回复
热议问题