Jan-31-2023, 08:57 PM
Greetings!
I hope I'm not overstaying my welcome here...
Anyway, I’m looking for a set of lines from a file.
See the example of a file below, the lines of interest are “TEST Started” and “TEST end”.
For each ‘TEST Started’ line there are multiple ‘TEST end’
I’d like to print the Last occurrence of the ‘TEST end’ line for each ‘TEST Started’’ line.
I got a partially working script, for each “Test started” line it prints the First occurrences of the “TEST end”
File example:
Script:
Thank you.
I hope I'm not overstaying my welcome here...
Anyway, I’m looking for a set of lines from a file.
See the example of a file below, the lines of interest are “TEST Started” and “TEST end”.
For each ‘TEST Started’ line there are multiple ‘TEST end’
I’d like to print the Last occurrence of the ‘TEST end’ line for each ‘TEST Started’’ line.
I got a partially working script, for each “Test started” line it prints the First occurrences of the “TEST end”
File example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
[ 7 : 02 : 27 AM 4 / 20 / 2020 ] some stuf [ 3 : 12 : 55 PM 4 / 20 / 2020 ] TEST Started < - - 1 [ 3 : 12 : 57 PM 4 / 20 / 2020 ] some stuf [ 3 : 12 : 59 PM 4 / 20 / 2020 ] some stuf [ 3 : 35 : 47 PM 4 / 20 / 2020 ] TEST end [ 3 : 35 : 48 PM 4 / 20 / 2020 ] TEST end [ 3 : 35 : 49 PM 4 / 20 / 2020 ] TEST end < - - - - 1 = = = = = = = = = = = = = = = = = = = = = = [ 3 : 51 : 23 PM 4 / 20 / 2020 ] some stuf [ 3 : 51 : 24 PM 4 / 20 / 2020 ] some stuf [ 3 : 52 : 23 PM 4 / 20 / 2020 ] TEST Started < - - 2 [ 3 : 55 : 25 PM 4 / 20 / 2020 ] some stuf [ 3 : 56 : 18 PM 4 / 20 / 2020 ] some stuf [ 3 : 56 : 19 PM 4 / 20 / 2020 ] some stuf [ 3 : 56 : 20 PM 4 / 20 / 2020 ] some stuf [ 3 : 56 : 21 PM 4 / 20 / 2020 ] some stuf [ 3 : 56 : 22 PM 4 / 20 / 2020 ] TEST end [ 3 : 57 : 23 PM 4 / 20 / 2020 ] TEST end [ 4 : 15 : 48 PM 4 / 20 / 2020 ] TEST end [ 4 : 15 : 49 PM 4 / 20 / 2020 ] TEST end < - - - - 2 [ 4 : 15 : 50 PM 4 / 20 / 2020 ] some stuf = = = = = = = = = = = = = = = = = = = = = = [ 4 : 27 : 28 PM 4 / 20 / 2020 ] some stuf [ 4 : 28 : 25 PM 4 / 20 / 2020 ] some stuf [ 4 : 29 : 29 PM 4 / 20 / 2020 ] some stuf [ 4 : 30 : 12 PM 4 / 20 / 2020 ] some stuf [ 4 : 44 : 14 PM 4 / 20 / 2020 ] TEST Started < - - 3 [ 4 : 44 : 14 PM 4 / 20 / 2020 ] some stuf [ 4 : 44 : 15 PM 4 / 20 / 2020 ] some stuf [ 4 : 44 : 16 PM 4 / 20 / 2020 ] TEST end [ 4 : 44 : 20 PM 4 / 20 / 2020 ] TEST end < - - - - 3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
with open ( 'c:/01/somefile.txt' , 'r' ) as fl : ts_start = '' ts_end = '' for el in fl : el = el.strip() if 'TEST Started' in el : ts_start = el elif 'TEST end' in el : ts_end = el if ts_start! = ' ' and ts_end!=' ' : print ( f " START - :{ts_start}, END -- {ts_end}" ) ts_start = '' ts_end = '' |