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

  • SEARCH
  • Home
  • 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 541843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:23:05+00:00 2026-05-13T10:23:05+00:00

I have a huge text file, which is structured as: SEPARATOR STRING1 (arbitrary number

  • 0

I have a huge text file, which is structured as:

SEPARATOR
STRING1
(arbitrary number of lines)
SEPARATOR
...
SEPARATOR
STRING2
(arbitrary number of lines)
SEPARATOR
SEPARATOR
STRING3
(arbitrary number of lines)
SEPARATOR
....

What only changes between the different “blocks” of the file is the STRING and the content between the separator. I need to get a script in bash or python which given a STRING_i in the input, gives as output a file, which contains

SEPARATOR
STRING_i
(number of lines for this string)
SEPARATOR

What is the best approach here to use bash or python? Another option? It must also be fast.

Thanks

  • 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-13T10:23:06+00:00Added an answer on May 13, 2026 at 10:23 am

    In Python 2.6 or better:

    def doit(inf, ouf, thestring, separator='SEPARATOR\n'):
      thestring += '\n'
      for line in inf:
        # here we're always at the start-of-block separator
        assert line == separator
        blockid = next(inf)
        if blockid == thestring:
          # found block of interest, use enumerate to count its lines
          for c, line in enumerate(inf):
            if line == separator: break
          assert line == separator
          # emit results and terminate function
          ouf.writelines((separator, thestring, '(%d)' % c, separator))
          inf.close()
          ouf.close()
          return
        # non-interesting block, just skip it
        for line in inf:
          if line == separator: break
    

    In older Python versions you can do almost the same, but change the line blockid = next(inf) to blockid = inf.next().

    The assumptions here are that the input and output files are opened by the caller (which also passes in the interesting values of thestring, and optionally separator) but it’s this function’s job to close them (e.g. for maximum ease of use as a pipeline filter, with inf of sys.stdin and ouf of sys.stdout); easy to tweak if needed of course.

    Removing the asserts will speed it up microscopically, but I like their “sanity checking” role (and they may also help understand the logic of the code flow).

    Key to this approach is that a file is an iterator (of lines) and iterators can be advanced in multiple places (so we can have multiple for statements, or specific “advance the iterator” calls such as next(inf), and they cooperate properly).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 347k
  • Answers 347k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The result of a modulo division is the remainder of… May 14, 2026 at 6:15 am
  • Editorial Team
    Editorial Team added an answer please check regular expresions linq with regex May 14, 2026 at 6:14 am
  • Editorial Team
    Editorial Team added an answer The main function you want to use is sys.source(), which… May 14, 2026 at 6:14 am

Related Questions

Given a text file, how would I go about reading an arbitrary line and
Currently migrating from SQL Server to PostgreSQL and attempting to improve a couple of
Greetings fellow earthlings. I'm researching methods of caching data retrieved from various database tables
I have a set of flags which are part of a huge text data
The application we have takes large amount of data most of which is called

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.