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