Home > Software design >  How to count multiple word occurrences in Unix coming after some pattern
How to count multiple word occurrences in Unix coming after some pattern

Time:03-17

FILE CONTENT:
2022-02-02 13:00:01 ERROR SINGAPORE
2022-02-02 13:00:01 ERROR HONGKONG
2022-02-02 13:00:01 ERROR SINGAPORE
2022-02-02 13:00:01 ERROR THAILAND
2022-02-02 13:00:01 ERROR HONGKONG
...
...

TOTAL SAY 800 ERRORS WITH 50 DIFFERENT COUNTRIES

QUERY - count different countries based on pattern ERROR.

REQUIRED OUTPUT -

SINGAPORE - 2
HONGKONG - 2
THAILAND - 1

How to do above in Unix with single line command ? I tried CUT but cannot count.

CodePudding user response:

With awk:

awk '{  a[$4]}END{for (country in a){print country" - "a[country]}}' yourfile.txt

That captures each country into an array index/key and increments that key's value each time that country is found. Then it prints out the array.

CodePudding user response:

You can use awk:

awk '$3="ERROR"{cnt[$4]  }
END{for (e in cnt) print e, cnt[e]}' file

If you want cut to 'count' you can do:

cut -d " " -f 4 file | sort | uniq -c
  • Related