On Sat, 21 Jul 2018 17:37:00 +0100, MRAB wrote: > On 2018-07-21 15:20, aldi.kr...@gmail.com wrote: >> Hi, >> I have a long text, which tells me which files from a database were >> downloaded and which ones failed. The pattern is as follows (at the end of >> this post). Wrote a tiny program, but still is raw. I want to find term >> "ERROR" and go 5 lines above and get the name with suffix XPT, in this first >> case DRXIFF_F.XPT, but it changes in other cases to some other name with >> suffix XPT. Thanks, Aldi >> >> # reading errors from a file txt >> import re >> with open('nohup.out', 'r') as fh: >> lines = fh.readlines() >> for line in lines: >> m1 = re.search("XPT", line) >> m2 = re.search('ERROR', line) >> if m1: >> print(line) >> if m2: >> print(line) >> > Firstly, you don't need regex for something has simple has checking for > the presence of a string. > > Secondly, I think it's 4 lines above, not 5. > > 'enumerate' comes in useful here: > > with open('nohup.out', 'r') as fh: > lines = fh.readlines() > for i, line in enumerate(lines): > if 'ERROR' in line: > print(line) > print(lines[i - 4])
Where's awk when you need it? import fileinput for line in fileinput.fileinput('nohump.out'): if 'XPT' in line: line_containing_filename = line if 'ERROR' in line: print(line_containing_filename) I think Aldi's original approach is pretty good. -- https://mail.python.org/mailman/listinfo/python-list