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

the input file is

and then I'd like to get this output:  

USING AWK is preferred

thank you all! :)


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.

| improve this answer | |

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 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
bash-4.1$ sort iplist.txt | uniq -d
| improve this answer | |
  • is this really can select items that occur 3 times or more also? – Arash 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 – Arash 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

For 4 times or more try:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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