Interview Test-Script-1: Use shell scripts to output the most logged-in users

Source: Internet
Author: User

Original topic:

A text-type file in which each line holds the IP of a lander (some lines are duplicated) and writes a shell script that outputs the most logged-on users.

Just saw this topic, immediately did not think of a straight line solution, although know can be sorted first, but later because of the parameters of the Uniq command is not familiar with, so use the method of comparison back, is directly write shell script program to solve this problem.

Now suppose the test data is as follows:

111.111.111.11110.10.10.10222.222.222.222111.111.111.111333.333.333.33310.10.10.10111.111.111.111333.333.333.33310.10.10.1 0222.222.222.222333.333.333.33310.10.10.10111.111.111.111222.222.222.222333.333.333.33310.10.10.10111.111.111.111111.111.1 11.111333.333.333.333333.333.333.333333.333.333.333222.222.222.22210.10.10.10222.222.222.222111.111.111.111333.333.333.33 3333.333.333.333111.111.111.11110.10.10.10333.333.333.333222.222.222.222222.222.222.222222.222.222.222222.222.222.222111.1 11.111.111111.111.111.111333.333.333.333333.333.333.333333.333.333.333333.333.333.333

The script to resolve this issue is as follows:

#!/bin/cshset lines= ' sort $ ' set I=1set num= ' Cat $ | Wc-l ' Set Max=0set cur= "" Set Counter=1set ip= "" while ($i <= $num  ) if ($LINES [$i]! = $cur) thenif ($max < $co Unter  ) thenset max= $counterset ip= $curendifset counter=1set cur= $LINES [$i]else@ counter++endif@ i++endif ($max < $counter) Thenset max= ip= $curendifecho $ip


First, the IP address is sorted, after sorting to find the most repeated number of the IP address on it. This logic is still relatively simple.

But if you know the number of times you can output a repetition with the Uniq command, the problem becomes simple:

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

The UNIQ-C command will output the first column as a repetition, and then we use the-K designation of the sort command to sort by the first column,-n means to use the numeric sort method instead of the default string ordering, and-R for descending output. Use head to output the first line, and then awk to output the second column is the most repeated IP address

Interview Test-Script-1: Use shell scripts to output the most logged-in users

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.