import re
def count_patt(fname, patt): result = {} cpatt = re.compile(patt) fobj = open(fname) for line in fobj: m = cpatt.search(line) if m: key = m.group() if key not in result: result[key] = 1 else: result[key] += 1 fobj.close() return result
if name == 'main': fname = '/var/log/httpd/access_log' ip_patt = '^(\d+.){3}\d+' br_patt = 'Firefox|Chrome|MSIE' print count_patt(fname, ip_patt) print count_patt(fname, br_patt)