Search and count patterns

Hi,
I have a text file the contents are like this

now i want to search patterns Z [720], Z [0] etc and count the occurrence of such patterns, after Z value can be any random digits, please help me it is urgent...

output like this
Z [0].............>5
Z [33]............>8

Try:

egrep -c '^Z \[[0-9]+\]' file
1 Like

thanks it shows total occurrence i want the occurrence of each pattern

Try:

awk '/^Z \[[0-9]+\]/{A[$0]++} END{for(i in A) print i " .....> " A}' file

---edit----

More robust version:

awk '{$1=$1} /^Z \[[0-9]+\]/{A[$0]++} END{for(i in A) print i " .....> " A}' file
1 Like

THANKS scrutinizer it worked wonderfully

one more version
try

$ awk '/^Z/{_[$1$2]++}END{for (i in _)print i,_}' OFS=\\t file

resulting

Z [740]    1
Z [0]      5
Z [720]    2

---------- Post updated at 05:46 AM ---------- Previous update was at 05:42 AM ----------

its producing

$ awk '/^Z \[[0-9]+\]/{A[$0]++} END{for(i in A) print i " .....> " A}' file
Z [740] .....> 1
Z [0] .....> 4
Z [720] .....> 2
Z [0]  .....> 1
2 Likes

Yes I realized it was not so robust, if there is any spurious spacing. I had added a more robust version to my post..

Thank you Scrutinizer... I just seen sorry

Don't be sorry, thanks for pointing it out, it is a valid point!

Another way:

$ egrep -o '^Z \[[0-9]+\]' File | sort | uniq -c
      5 Z [0]
      2 Z [720]
      1 Z [740]

Regards.