Python Forum
Getting last line of each line occurrence in a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting last line of each line occurrence in a file
#1
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:
[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 
Script:
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 = ''   
Thank you.
Reply
#2
Your logic is backward. You should present results when you encounter a start, not an end. It is omly when you see START, or run out of lines, that you know you found the last end.
with open('data.txt', 'r') as file:
    start = end = None
    for line in map(str.strip, file):
        if 'TEST Started' in line:
            if end:
                # Print previous start/end pair
                print(start, end, sep="\n", end="\n\n")
            start = line
            end = None
        elif 'TEST end' in line:
            end = line
    if start and end:
        print(start, end, sep="\n")
tester_V likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No new line from print in a browser Lou 13 1,366 Dec-01-2025, 06:39 PM
Last Post: noisefloor
  cannot unpack non-iterable int object in urllib3/util/wait.py", line 85, ping_chen_ibm_us 2 931 Aug-01-2025, 02:05 PM
Last Post: ping_chen_ibm_us
  [SOLVED] Why does regex fail cleaning line? Winfried 7 4,937 Jul-11-2025, 11:52 PM
Last Post: Pedroski55
  I am a newbie I like to use the command line Mikel2025 1 1,063 Jun-13-2025, 03:20 PM
Last Post: Gribouillis
  Βad Input on line 12 Azdaghost 5 2,237 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 967 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Insert command line in script lif 4 2,200 Mar-24-2025, 10:30 PM
Last Post: lif
  Entry field random pull from list, each return own line Bear1981 6 1,940 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  How to revert back to a previous line from user input Sharkenn64u 2 3,184 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Pandas - error when running Pycharm, but works on cmd line zxcv101 2 3,595 Sep-09-2024, 08:03 AM
Last Post: pinkang

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020