Python Forum
If match not found print last line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If match not found print last line
#1
Greetings!
I need to print the last line of a file if a match (second match) not found.
I'm printing all the lines with "match-2" for each "match-1" line.

line-1 match-1
line something else
line something else
line-4 match-2
line-5 match-2
line-6 match-2
line something else
line something else
line something else
line something else
line-11 match-1
line something else
line something else
line-14 match-2
line something else
line something else
line-17 last match-1
line-18 last need to print

Here is the code I got so far:
with open('C:/02/file2.txt','r') as  mf :
    for el in mf :
        el=el.strip()

        if 'match-1' in el :
            one=el.strip()
            #print ("one "+one)
        elif 'match-2' in el :
            tr = el 
            print(one+" , "+tr)     
Code prints :

line-1 match-1 , line-4 match-2
line-1 match-1 , line-5 match-2
line-1 match-1 , line-6 match-2
line-11 match-1 , line-14 match-2

Need also to print :
line-17 last match-1 , line-18 last need to print

Thank you!
Reply
#2
Maybe not the best way but might help

#! /usr/bin/env python3
matches = []
with open('file.txt') as file:
    lines = file.readlines()
for line in lines:
    if 'match-1' in line or 'match-2' in line:
        matches.append(line)
        print(line.strip())
if len(matches) % 2 != 0:
    print(lines[-1])
Output:
line-1 match-1 line-4 match-2 line-5 match-2 line-6 match-2 line-11 match-1 line-14 match-2 line-17 last match-1 line-18 last need to print
tester_V likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
I like your thought!
It is great but I need to go with a line-by-line checking the file, it is not a small file and no tone file.
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 374 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Print the line before the corrent line tester_V 9 1,553 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  Need to match two words in a line tester_V 2 868 Nov-18-2022, 03:13 AM
Last Post: tester_V
  Match and extract if found Calli 21 3,747 Sep-16-2022, 07:42 AM
Last Post: Gribouillis
  Print to a New Line when Appending File DaveG 0 1,217 Mar-30-2022, 04:14 AM
Last Post: DaveG
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,533 Nov-23-2020, 05:15 PM
Last Post: cananb
  print a line break in writelines() method leodavinci1990 1 6,441 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,019 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 5,718 Sep-30-2020, 02:53 PM
Last Post: buran
  Need print out lines before and after the match tester_V 6 6,848 Aug-11-2020, 02:50 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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