Python Forum
print only last matched line
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print only last matched line
#21
1. My examples all start with code you provided. Flag and cnt are from your code. They are where they are because that is where you put them. Flag does nothing, I don't know why it was in your code. cnt could be initialized at the top of the program or inside the with. It works fine in either location.

2. cnt was your variable from your code. It counts how many lines were read. I assumed since you were counting lines that you wanted to know the line number of log entries that matched lstart and lstr.

2.a. the readLine locations are again from code that you provided. They work fine, so I did not change them. There are lots of ways you could read all the lines from the log file. For example:
while True:
    line = fp.readline()
    if not line:
        break:  # Break out of loop
    elif lstart in line:
Or using the new walrus operator available in Python 3.8
while line := fp.readline():  # Loop until all lines were read
    if lstart in line:
Reply
#22
"2.a. the readLine locations are again from code that you provided. They work fine, so I did not change them. There are lots of ways you could read all the lines from the log file. For example:"
I can read all lines from the file all day ling, it is not the problem reading all lines but printing only ones I want.
For example, I did not declare these two:
"last_lstart"
"last_lstr" 
I tried to print them - no luck.

Thank you>
Reply
#23
f_toread = 'logfile.txt'
lstr  = '--InitializeRequest'
lstart = "--LotComplete"
last_lstart = None
last_lstr = None
   
with open(f_toread) as fp:  
    line = fp.readline()
    while line:
        if lstart in line:
            # Did I find a pair?
            if last_lstart is not None and last_lstr is not None:
                # I found a pair!
                print(last_lstart, end = '')
                print(last_lstr)
            last_lstart = line
            last_lstr = None
        elif lstr in line:
            last_lstr = line
        line = fp.readline()
Reply
#24
God damn it!
I spent a week and could not make it work but now I see it after you did it.
This part:
last_lstart = line
last_lstr = None
is totaly beyond me for today...very interesting move...awesome.

Thank you again!
Reply
#25
last_lstr is set to None to indicate that we have not found a second lines to pair with the most recent first line.

And watch the cursing The appropriate response is a solid head smack.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 393 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Print the line before the corrent line tester_V 9 1,575 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  failing to print not matched lines from second file tester_V 14 6,104 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,226 Mar-30-2022, 04:14 AM
Last Post: DaveG
  If match not found print last line tester_V 2 2,895 Apr-26-2021, 05:18 AM
Last Post: tester_V
  print a line break in writelines() method leodavinci1990 1 6,475 Oct-12-2020, 06:36 AM
Last Post: DeaD_EyE
  Print characters in a single line rather than one at a time hhydration 1 2,039 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 5,768 Sep-30-2020, 02:53 PM
Last Post: buran
  Pattern Require Last Line Print() why? Harshil 4 2,443 Aug-08-2020, 04:54 PM
Last Post: Harshil
  Python re.sub text manipulation on matched contents before substituting xilex 2 2,118 May-19-2020, 05:42 AM
Last Post: xilex

Forum Jump:

User Panel Messages

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