I'm trying to count the number of occurrences of multiple text patterns in a log file.
My code is counting all the lines in the file for all text patterns.
Where am I going wrong?
The log file looks like this:
Feb 1 00:00:02 bridge kernel: INBOUND TCP: IN = br0 PHYSIN = eth0 OUT = br0> PHYSOUT = eth1 SRC = XXX.XXX.XXX.XXX DST = XXX.XXX.XXX.XXX LEN = 40 TOS = 0x00> PREC = 0x00 TTL = 110 ID = 12973 PROTO = TCP SPT = 220 DPT = 6129 WINDOW = 16384 RES = 0x00> SYN URGP = 0
Feb 1 00:00:02 bridge kernel: INBOUND TCP: IN = br0 PHYSIN = eth0 OUT = br0> PHYSOUT = eth1 SRC = XXX.XXX.XXX.XXX DST = XXX.XXX.XXX.XXX LEN = 40 TOS = 0x00> PREC = 0x00 TTL = 113 ID = 27095 PROTO = TCP SPT = 220 DPT = 6129 WINDOW = 16384 RES = 0x00> SYN URGP = 0
My code at the moment looks like this:
#!//usr/bin/python3
import sys
import os
import re
tipos= set()
cnt=0
p= re.compile ('bridge kernel:.*:')
with open (sys.argv[1], 'r') as f:
for line in f:
match = p.search(line)
if match:
taux=(line.split(":") [3])
tipos.add(taux)
if taux in tipos:
cnt+=1
elif taux not in tipos:
cnt=0
d=dict.fromkeys(tipos,contador)
print (d)