Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

123.33.22.33
221.23.128.2
123.33.22.33
92.222.192.12
92.222.192.12
123.33.22.33

I can sort it to:

123.33.22.33
123.33.22.33
123.33.22.33
221.23.128.2
92.222.192.12
92.222.192.12

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?

thanks

share|improve this question
add comment

3 Answers

up vote 5 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

e.g.

root@pinkpony:~# cat /tmp/xx
123.33.22.33
221.23.128.2
123.33.22.33
92.222.192.12
92.222.192.12
123.33.22.33
root@pinkpony:~# cat /tmp/xx | sort -nr | uniq -c | sort -nr -k1
      3 123.33.22.33
      2 92.222.192.12
      1 221.23.128.2
root@pinkpony:~# 
share|improve this answer
add comment

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
add comment

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

cat sorted.list | uniq -c

  2 1.2.3.4
  1 5.6.7.8

That'll tally the counts for you.

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.