こちらを参考にしました
http://www.freia.jp/taka/blog/356
import re f = open("error_log") pattern = re.compile(r"\[client (?P<ip>[0-9\.]+)\]") ips = {} for line in f: m = pattern.search(line) if m: ip = m.group("ip") ips[ip] = ips.get(ip, 0) + 1 d = [(v,k) for k,v in ips.items()] d.sort() d.reverse() total = 0 for count, ip in d[:20]: total += count print count, ip print "total: %s" % total