Discussion: counting ip addresses
Afficher un message
Vieux 27/10/2006, 16h36   #4
Stephan Grein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: counting ip addresses

rogv24@yahoo.com wrote:
> I am trying to write a routine to provide counts from a list of IP
> addresses that are sorted.
>
> example:
>
> 2.3.4.5
> 2.3.4.5
> 2.3.4.5
> 10.20.20.40
> 10.20.20.40
>
> report would like this 2.3.4.5 - sum 3
> 10.20.20.40 - sum 2
>
> thanks
>


If you don't mind Python:

#!/usr/bin/python
# emulates uniq -c
# *-* encoding: utf-8 -*-

import sys

res = {}
count = 1
for x in open(sys.argv[1], "r").readlines():
try:
res[x] += 1
except:
res[x] = 1

for key, value in res.items():
print "#%i %s occured %i times!"%(count, key.replace("\n", ""), value)
count += 1
res = {}

Or just use uniq and the -c switch!

HTH
--
Stephan Grein, <stephan at stephan minus rockt dot de>
https://stephan-rockt.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
  Réponse avec citation
 
Page generated in 0,04668 seconds with 9 queries