regex - Python error log pattern matching -


i python newbie , want learn hardway. writing function extract content between patterns. log file construct follows

<time-stamp>[begin cache] <...some content> <time-stamp>.  <time-stamp>. <time-stamp>. <time-stamp>. <time-stamp>[error] <..some content> <time-stamp>. <time-stamp>. <time-stamp>. <time-stamp>. <time-stamp>[end cache] <....some content> <time-stamp>. <time-stamp>. <time-stamp>. <time-stamp>[begin cache] <... content> <time-stamp>. <time-stamp>. <time-stamp>. <time-stamp>[end cache] <... content> 

i interested in extracting part between begin cache , end cache if there pattern error between them. code have written far no way getting me goal. logic used find positions of begin cache pattern , end cache pattern if error tag present , print file between positions. appreciated.

import re import os import mmap file="\\\\xxxxx\c$\ego\soam\work\xxxx_20140307.03\dal_xxxx_yyyy_20140320_110536_21_6508.log" open(file,"r") file:     m=mmap.mmap(file.fileno(),0,access=mmap.access_read)     mpattern="\[error\]"     spattern="begin cache"     epattern="end cache"     mregexp=re.compile(mpattern)     sregexp=re.compile(spattern)     eregexp=re.compile(epattern)     match in sregexp.finditer(m):         epos=eregexp.match(m,match.end())         if mregexp.match(m,match.end(),epos):             print("%s"%(m,match.start(),epos)) 

i wish have tutorials fast start incredibly simple yet confusing language.

you can scan log file [error] , text needed regex used split data read logfile. suggest example method:

edit after data format changed:

use regex : \[[^r]\w+\s\w+\] split list , view error part in example:

import re f = open('logfile', 'r') data = f.read() f.close() mylist = re.split(r'\[[^r]\w+\s\w+\]',data) item in mylist:     if '[error]' in item:         print item 

edit:

some places learn more python:

learnpython.org

python.org/tutorial

sthurlow.com/python

hope helped.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -