Hi,

I'm pretty new to python but thought I'd write something to read this log file to try and learn it. Ideally, it will read this log file, match a string and return everything between the "****" delimiters where the string was matched. I'm not 100% sure how to go about this though. I populate my tuple with the following, but can't see how I display the lines inbetween the delimiter objects.

def look(self):
        
        NULL = None
        data = [(0,0,0,0)] ## (LINE #, STRING, Match_Obj, Del_Obj)       
        segment = re.compile('[*****]')
        lookfor = re.compile('[COMMS_MESSAGE_SENT]')
        LineNum = 0
        
        for line in self.thefilesText:
            obj = lookfor.match(line)
            deli = segment.match(line)
            data.append((LineNum,line,obj,deli))
            LineNum += 1  
        
        return data

I could be going about this the wrong way. Should I be segmenting the log file fist (between delimiters), eg, 1 segment per list element
and then using RE to search the segments (then return the entire segment when found).

any input would be great!

Recommended Answers

I'd store the blocks in a list, looking for the pattern in each line and, if i find the pattern in one line, write the whole block out.

isBlockToWrite=False
pattern="COMMS_MESSAGE_SENT"
block=[]
outfile=file("log3.log","w")
for line in file("log2.log"):
    block.append(line)
    if line[:10] == "*" * 10:
        if isBlockToWrite:
            outfile.writelines(block)
        block=[] …
Jump to Post

All 2 Replies

I'd store the blocks in a list, looking for the pattern in each line and, if i find the pattern in one line, write the whole block out.

isBlockToWrite=False
pattern="COMMS_MESSAGE_SENT"
block=[]
outfile=file("log3.log","w")
for line in file("log2.log"):
    block.append(line)
    if line[:10] == "*" * 10:
        if isBlockToWrite:
            outfile.writelines(block)
        block=[]
        isBlockToWrite=False
    if pattern in line:
        isBlockToWrite=True

I was going about this the wrong way, thx so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.