user393267
user393267

Reputation:

how to find a string in a text file and print the lines right before and after

I am trying to convert my shell scripts into python code, but I am stuck while trying this operation.

I have a process that outputs a text file, the file has sections like this:

Running Operation kdasdakdnaskdaksdma

 (error if present) error: kdmakmdasmdaksom

This file could have multiple lines for the operation and the error (if present, otherwise the next line will just have another operation); there is always a crlf after each block.

I am trying to scan the file to find the line that contains "error:", and then read the operation that caused the error and the details of the error, so i can extrapolate it from the text file and save it in an error log file.

So far i can find the line(s) that has "error:" in it, with this simple code, but I am not able to find any example about how do you actually print the lines that are not necessarily the ones that contain the error message, but the ones that came before and after the line where "error:" is located.

using awk or grep would be straightforward, but with Python I am not really sure about how to do so; this is what i have so far, that is able to print the line that has the error but it prints just that, while i would like to have control to the lines printed before and after.

import re

fh = open('~/logs_output.txt')

for line in fh:
    if "error:" in line:
        print line

Tried to look at RE module in python, and also to the string modules, but so far I haven't found anything that would allow me to do what you would do with awk for example, where you can look for an occurrence of a specific string and turn on print, and then turn it off, once that you are done

Can anyone point me to the right direction to tackle this issue? Thanks!

Upvotes: 1

Views: 16480

Answers (1)

eyquem
eyquem

Reputation: 27575

import re


ss = '''qhvfgbhgozr
yytuuuyuyuuuyuyuuyy
jhfg tryy error  jjfkhdjhfjh ttrtr
aaaeeedddeedaeaeeaeeea
jhzdgcoiua zfaozifh cohfgdyg fuo'''

regx = re.compile('^(.*)\r?\n(.*?error.*)\r?\n(.*)', re.MULTILINE)

print regx.search(ss).groups()

result

('yytuuuyuyuuuyuyuuyy', 'jhfg tryy error  jjfkhdjhfjh ttrtr', 'aaaeeedddeedaeaeeaeeea')

Upvotes: 1

Related Questions