How do I print out the count of unique matches with grep?

后端 未结 1 1891
悲哀的现实
悲哀的现实 2021-02-05 05:41

Lets say I have millions of packets to look through and I want to see how many times a packet was sent to a certain port number.

Here are some of the packets:

         


        
相关标签:
1条回答
  • 2021-02-05 05:58

    See uniq -c. You'll want to pull out the bit you want, sort the result, pipe thru uniq, sort the output. Something like this maybe:

    egrep '\.[0-9]+:' output.txt | sort | uniq -c | sort -nr
    

    Clarification: I've used grep here because it's not clear what your output.txt format looks like, but you'll want to actually cut out the port number bit, perhaps via cut or awk.

    Edit: To get the port, you can cut once on a period and then again on a colon:

    cut -d. -f10 < output.txt | cut -d: -f1
    

    (Or any one of a dozen other ways to accomplish the same thing.) That will give you an unsorted list of ports. Then:

    cut -d. -f10 < output.txt | cut -d: -f1 | sort | uniq -c | sort -nr
    
    0 讨论(0)
提交回复
热议问题