How can I get the previous row and next row in a log file

Viewed 39

I write a python program. This program outputs the lines from the log file by filtering through GUI Tkinter.

I made some features like when I want to type a word only, it extracts the lines that word has.

I am struggling with the new feature. I want it to add the line before and after the line I want to print.

My Search function is like this but beforeLine and afterLine doesn't work.

#function to search string in text
def search(msg, startingDate, endingDate, beforeLine, afterLine):
    # clear current result
    text.delete('1.0', 'end')
    with open(file, 'r', encoding='latin1') as fp:
        for l_no, line in enumerate(fp, 1):
            if msg and msg not in line:
                # does not contain search message, skip it
                continue
            if startingDate or endingDate:
                # get the timestamp
                timestamp = parse_date(line[1:25])
                # within startingDate and endingDate ?
                if startingDate and timestamp < startingDate:
                    # before given starting date, skip it
                    continue
                if endingDate and timestamp > endingDate:
                    # after given ending date, skip it
                    continue
                if beforeLine == 1 or afterLine == 1:
                    text.insert('end', f'Before Line Number: {l_no} Before Line: {line[:1]}')
                    text.insert('end', f'After Line Number: {l_no} After Line: {line[+1]}')
                    continue
            # insert the log
            text.insert('end', ' \n ')
            text.insert('end', f'Line Number: {l_no} Log: {line}')
            text.insert('end', ' \n ')



def getInfo():
    msg = edit.get() if var1.get() == 1 else None
    startingDate = parse_date(tarih1.get()) if var2.get() == 1 else None
    endingDate = parse_date(tarih2.get()) if var3.get() == 1 else None
    beforeLine = beforeEntry.get() if varBefore.get() == 1 else None
    afterLine = afterEntry.get() if varAfter.get() == 1 else None
    
    search(msg, startingDate, endingDate, beforeLine, afterLine)
0 Answers
Related