1

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!

1
  • You should be able to use re for your purposes. In addition googling "python awk" brings up results which will probably be helpful to you. Commented Oct 14, 2011 at 23:36

1 Answer 1

1
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')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot eyquem! Could you please be so kind to give me some hints about the meaning of the RE that you wrote? So I can modify it in case i need more than just the first line before or after (in some cases the error has multiple lines,each one with a CRLF so with this RE i just get the first one. Thanks a lot!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.