Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4572226
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:37:00+00:00 2026-05-21T19:37:00+00:00

I would like to search through a text file and print out a line

  • 0

I would like to search through a text file and print out a line and its subsequent 3 lines if a keyword is found in the line AND a different keyword is found within the subsequent 3 lines.

My code right now prints too much information. Is there a way to move forward to the next section of text once a portion is already printed?

text = """

here is some text 1
I want to print out this line and the following 3 lines only once keyword 2
print this line since it has a keyword2 3
print this line keyword 4
print this line 5
I don't want to print this line but I want to start looking for more text starting at this line 6
Don't print this line 7
Not this line either 8
I want to print out this line again and the following 3 lines only once keyword 9
please print this line keyword 10
please print this line it has the keyword2 11
please print this line 12
Don't print this line 13
Start again searching here 14
etc.
"""

text2 = open("tmp.txt","w")
text2.write(text)
text2.close()

searchlines = open("tmp.txt").readlines()

data = []

for m, line in enumerate(searchlines):
    line = line.lower()
    if "keyword" in line and any("keyword2" in l.lower() for l in searchlines[m:m+4]):
        for line2 in searchlines[m:m+4]:
            data.append(line2)
print ''.join(data)

The output right now is:

I want to print out this line and the following 3 lines only once keyword 2
print this line since it has a keyword2 3
print this line keyword 4
print this line 5
print this line since it has a keyword2 3
print this line keyword 4
print this line 5
I don't want to print this line but I want to start looking for more text starting at this line 6
I want to print out this line again and the following 3 lines only once keyword 9
please print this line keyword 10
please print this line it has the keyword2 11
please print this line 12
please print this line keyword 10
please print this line it has the keyword2 11
please print this line 12
Don't print this line 13
please print this line it has the keyword2 11
please print this line 12
Don't print this line 13
Start again searching here 14

I would like it to print out only:

I want to print out this line and the following 3 lines only once keyword 2
print this line since it has a keyword2 3
print this line keyword 4
print this line 5
I want to print out this line again and the following 3 lines only once keyword 9
please print this line keyword 10
please print this line it has the keyword2 11
please print this line 12
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:37:00+00:00Added an answer on May 21, 2026 at 7:37 pm

    So, as someone else has pointed out, your first keyword keyword is a substring of your second keyword keyword2. So I’ve implemented this using regexp objects, so that you can use the word boundary anchor \b.

    import re
    from StringIO import StringIO
    
    text = """
    
    here is some text 1
    I want to print out this line and the following 3 lines only once keyword 2
    print this line since it has a keyword2 3
    print this line keyword 4
    print this line 5
    I don't want to print this line but I want to start looking for more text starting at this line 6
    Don't print this line 7
    Not this line either 8
    I want to print out this line again and the following 3 lines only once keyword 9
    please print this line keyword 10
    please print this line it has the keyword2 11
    please print this line 12
    Don't print this line 13
    Start again searching here 14
    etc.
    """
    
    
    def my_scan(data,search1,search2):
      buffer = []
      for line in data:
        buffer.append(line)
        if len(buffer) > 4:
          buffer.pop(0)
        if len(buffer) == 4: # Valid search block
          if search1.search(buffer[0]) and search2.search("\n".join(buffer[1:3])):
            for item in buffer:
              yield item
            buffer = []
    
    # First search term
    s1 = re.compile(r'\bkeyword\b')
    s2 = re.compile(r'\bkeyword2\b')
    
    for row in my_scan(StringIO(text),s1,s2):
      print row.rstrip()
    

    Produces:

    I want to print out this line and the following 3 lines only once keyword 2
    print this line since it has a keyword2 3
    print this line keyword 4
    print this line 5
    I want to print out this line again and the following 3 lines only once keyword 9
    please print this line keyword 10
    please print this line it has the keyword2 11
    please print this line 12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes i would like to search for text containing a new line character and
I would like to search through all of my procedures packages and functions for
I would like my python script to search through a directory in SVN, locate
I would like to search an HTML file for a certain string and then
I would like to know how to run a file like this: Search-Mailbox -Identity
I've got multiple SVN repositories of different projects which I would like to search
I've got a text file, with about 200,000 lines. Each line represents an object
I have a ~700 MB binary file (non-text data); what I would like to
I would like to be able to use Full text search in Code First
I would like to search my table having a column of first names and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.