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! :)

3 Answers 3


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.


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
  • is this really can select items that occur 3 times or more also?
    – Arash
    Mar 10, 2013 at 20:24
  • Yes, any line that appears more than once will listed once. Mar 10, 2013 at 20:27
  • no i want to clarify that show items whome appears more than x times not just onece
    – Arash
    Mar 10, 2013 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. Mar 10, 2013 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. Mar 10, 2013 at 21:01

For 4 times or more try:

awk '++A[$1]==4' file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .