Count number of occurrences in a log file

Asked

Viewed 44 times

1

I am trying to count the number of occurrences of multiple text patterns in a log file.

My code is counting all lines of 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 is 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)
  • I translated the question. Thank you.

1 answer

0

in row 18 Replace "counter" with "cnt"

  • Done. It was just a typo. Thank you.

  • show, happy to help. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.