regex - Include surrounding lines of text file match in output using Python 2.7.3 -
i've been working on program assists in log analysis. finds error or fail messages using regex , prints them new .txt file. however, more beneficial if program including top , bottom 4 lines around match is. can't figure out how this! here part of existing program:
def error_finder(filepath): source = open(filepath, "r").readlines() error_logs = set() my_data = [] line in source: line = line.strip() if re.search(exp, line): error_logs.add(line)
i'm assuming needs added last line, i've been working on bit , either not applying myself or can't figure out.
any advice or on appreciated.
thank you!
why python?
grep -c4 '^your_regex$' logfile > outfile.txt
Comments
Post a Comment