Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I select the ips that showed up 4 times or more , for example here?

the input file is

192.168.1.28
192.168.1.100
192.168.1.31
192.168.101.2
192.168.1.31
192.168.11.02
192.168.1.31
192.168.1.28
192.168.1.28
192.168.1.31
192.168.21.31
192.168.1.28
192.168.1.31

and then I'd like to get this output:

192.168.1.28
192.168.1.31  

USING AWK is preferred

thank you all! :)

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

If you combine Nicole Hamilton's answer and gawk you get (for lines that appear 4 times or more):

sort iplist.txt | uniq -c | gawk '$1>=4{print $2}' 

For exactly four times do :

sort iplist.txt | uniq -c | gawk '$1==4{print $2}' 

Change the 4 to whichever number you are interested in.

share|improve this answer
    
greaattttttttttt thank you :* :D –  Christopher Mar 10 '13 at 21:22
add comment

Let's assume the list in a file named iplist.txt. Try this:

sort iplist.txt | uniq -d

Here's what it looks like when you run it. I've deliberately added a 3rd occurrence of the IP address 192.168.1.28 so you can see that only the lines that occur 2 or more times get listed in the output, with each duplicated line shown just once.

bash-4.1$ cat iplist.txt
192.168.1.28
192.168.1.100
192.168.1.31
192.168.101.2
192.168.11.02
192.168.1.31
192.168.21.31
192.168.1.28
192.168.1.28
bash-4.1$ sort iplist.txt | uniq -d
192.168.1.28
192.168.1.31
bash-4.1$
share|improve this answer
    
is this really can select items that occur 3 times or more also? –  Christopher Mar 10 '13 at 20:24
    
Yes, any line that appears more than once will listed once. –  Nicole Hamilton Mar 10 '13 at 20:27
    
no i want to clarify that show items whome appears more than x times not just onece –  Christopher Mar 10 '13 at 20:30
    
You many need to rewrite your question to clarify what it is you want. You asked about lines that appear "2 times and more" and your example shows printing the two IP addresses that appeared exactly twice. My proposed solution gives that output on your sample input. And if you added 3rd, 4th, 5th, etc., occurrences of those same duplicated IP addresses, the output would be the same. –  Nicole Hamilton Mar 10 '13 at 20:34
1  
Then use uniq -c to get a count of the number of occurrences of each value then select the lines you want. awk would be perfect for that but I'll leave it to write that. –  Nicole Hamilton Mar 10 '13 at 21:01
show 4 more comments

For 4 times or more try:

awk '++A[$1]==4' file
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.