Use the most frequently occurring IP in the shell script statistics file

Source: Internet
Author: User

For example, there are the following files Test.txt

1 134.102.173.43

2 134.102.173.43

3 134.102.171.42

4 134.102.170.9

To count the most frequently occurring IPs, you can take advantage of the following shell script:

Cat Test.txt | awk ' {print $} ' | Sort | uniq-c | Sort-n-R | Head-n 1

[Original] The most frequently counted IP

One is still very common, very practical, very simple command

Netstat-ntuactive Internet Connections (w/o servers) Proto Recv-Q send-Q Local Address Foreign address statetcp0 0 127.0.0.1:8152 127.0.0.1:4193time_waittcp0 0 127.0.0.1:8152 127.0.0.1:4192time_waittcp0 0 127.0.0.1:8152 127.0.0.1:4196time_waittcp0 0 127.0.0.1:8152 127.0.0.1:4199time_waittcp0 0 127.0.0.1:8152 127.0.0.1:4201time_waittcp0 0 127.0.0.1:8152 127.0.0.1:4204time_waittcp0 0 127.0.0.1:8152 127.0.0.1:4207time_waittcp0 0 127.0.0.1:8152 127.0.0.1:4210time_waittcp0 0 192.168.32.62:41682 192.168.47.27:5431time_waittcp0 0 192.168.32.62:41685 192.168.47.27:5431Time_waitnetstat-ntu | Tail-n +3|awk'{print $}'| Cut-d:-F1| Sort | uniq-c| Sort-n-R | Head-n58 127.0.0.12 192.168.47.27

Tail-n +3: Remove the two lines marked in red above.

awk ' {print $} ': 5 fields (5th column) with low data, indicated by blue above.

Cut-d:-F 1: Take the IP section in front of the blue section.

Sort: Sorts the IP parts.

UNIQ-C: Prints the number of occurrences of each repeating row. (and remove duplicate lines)

Sort-n-R: In reverse order in which repeated rows appear.

Head-n 5: Take the first 5 bits of IP

Use the most frequently occurring IP in the shell script statistics file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.