Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
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 – Arash Mar 10 '13 at 21:22

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
share|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
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
share|improve this answer

Your Answer


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.