pattern block expression matching
dan at tombstonezero.net
Sat Jul 21 14:40:13 EDT 2018
On Sat, 21 Jul 2018 17:37:00 +0100, MRAB wrote:
> On 2018-07-21 15:20, aldi.kraja at gmail.com wrote:
>> 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:
>> if m2:
> 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(lines[i - 4])
Where's awk when you need it?
for line in fileinput.fileinput('nohump.out'):
if 'XPT' in line:
line_containing_filename = line
if 'ERROR' in line:
I think Aldi's original approach is pretty good.
More information about the Python-list