[Tutor] re matching
Michael Montagne
Michael Montagne <montagne@boora.com>
Thu May 1 15:48:02 2003
I'm trying to quickly parse some errors out of mail.log.
Well, ok, the quickly part is already a lost cause.
Here is my program:
import re
fname="/home/montagne/mail.log.0"
pattern=".*Abandoning.*"
logfile=open(fname,"r")
badlist=[]
cnt=1
maxcnt=200
loglines=logfile.readlines()
logfile.close()
for line in loglines:
if cnt > maxcnt:
break
#print line
omatch=re.search(pattern,line)
if omatch:
sDate=line.split("]:")[0]
sDate=sDate + "]"
print sDate
badlist=badlist + [sDate]
cnt=cnt + 1
print str(len(badlist)) + " bad things"
for line in badlist:
line1=line.replace(":","\\\\:")
line2=line1.replace("]","\\\\]")
line3=line2.replace("[","\\\\[")
pattern=line3
print pattern
cnt=0
for line in loglines:
if cnt > maxcnt:
break
p=re.compile(pattern)
omatch=p.match(line)
if omatch:
#sOutstring=line[88:]
sOutstring=line
print sOutstring
cnt = cnt + 1
print "Done"
It should make a list of strings representing lines with the word "Abandoning" on them. Then for each of those strings, look through the same list of strings for those matching the string. But I can not generate a match as I cycle through badlist. If I plug the strings into the IDLE python shell, they appear to work.
I must be missing something.