Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

I've got a huge textfile which contains IP addresses:

I can sort it to:

and see (with bare eye) that first IP occurs three times, second once, and last one twice.

I'd like to be able to do this in huge logfiles, obviously in an automated way. Is it possible?


share|improve this question
up vote 7 down vote accepted
cat file_with_ips | sort -nr | uniq -c | sort -nr -k 1

will sort desc by ip counts, showing the counters on first column


root@pinkpony:~# cat /tmp/xx
root@pinkpony:~# cat /tmp/xx | sort -nr | uniq -c | sort -nr -k1
share|improve this answer

Once you have it sorted you can pipe it throu uniq -c

cat sorted.list | uniq -c


That'll tally the counts for you.

share|improve this answer

You can use uniq -c to get a count, but be sure to sort the input first or you'll only get counts of consecutive, alike lines:

sort yourfile | uniq -c

I often then sort it by the lines with the most matches:

sort yourfile | uniq -c | sort -nr
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.