Trying to pull out paired lines, LINE 1 and LINE 2 from a file for farther processing.
The file goes like this:
Some lines
LINE 1
Some lines
Some lines
LINE 2
LINE 2
LINE 2
Some lines
Some lines
Some lines
LINE 1
Some lines
LINE 2
And so on.
I’d like to print:
LINE 1
LINE 2 (preferably last of LINE 2)
Also, if after the last LINE 1 no LINE 2 found I’d like to print the last line of the file.
The Python3.x code I’m come up so far prints All LINE 1 and LINE 2 not what I want.
The file goes like this:
Some lines
LINE 1
Some lines
Some lines
LINE 2
LINE 2
LINE 2
Some lines
Some lines
Some lines
LINE 1
Some lines
LINE 2
And so on.
I’d like to print:
LINE 1
LINE 2 (preferably last of LINE 2)
Also, if after the last LINE 1 no LINE 2 found I’d like to print the last line of the file.
The Python3.x code I’m come up so far prints All LINE 1 and LINE 2 not what I want.
Python:
filepath = 'mytext1.txt'
line1 = 'LINE 1'
line2 = 'LINE 2'
with open(filepath) as fp:
line = fp.readline()
while line:
if line1 in line:
print("string found in line ---"+line)
if line2 in line:
print("string found in line ---"+line)
line = fp.readline()