(1)
import re
def count_patt(fname, patt):
patt_dict = {}
cpatt = re.compile(patt)
with open(fname) as fobj:
for line in fobj:
m = cpatt.search(line)
if m:
key = m.group()
patt_dict[key] = patt_dict.get(key, 0) + 1
return patt_dict
if name == ‘main‘:
fname = ‘access_log’
ip = ‘^(\d+.){3}\d+’
print(count_patt(fname, ip))
# awk ‘{print 1}’ access_log | sort | uniq -c | sort -nr
br = ‘Firefox|MSIE|Chrome’
print(count_patt(fname, br))
shell = ‘bash
1}’ access_log | sort | uniq -c | sort -nr br = ‘Firefox|MSIE|Chrome’ print(count_patt(fname, br)) shell = ‘bash
|nologin$’
print(count_patt(‘/etc/passwd’, shell))
(2)
import re
from collections import Counter
class CountPatt:
def init(self, fname):
self.fname = fname
def count_patt(self, patt):
cpatt = re.compile(patt)
patt_obj = Counter()
with open(self.fname) as fobj:
for line in fobj:
m = cpatt.search(line)
if m:
item = m.group()
patt_obj.update([item])
return patt_obj
if name == ‘main‘:
fname = ‘access_log’
ip = ‘^(\d+.){3}\d+’
web_log = CountPatt(fname)
a = web_log.count_patt(ip)
print(a)
print(a.most_common(3))
b = web_log.count_patt(‘Firefox|MSIE|Chrome’)
print(b)
print(b.most_common(5))