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 8975635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:55:48+00:00 2026-06-15T18:55:48+00:00

I have a file that is in the following format: Preamble ——————— Section 1

  • 0

I have a file that is in the following format:

Preamble

---------------------
Section 1
...
---------------------

---------------------
Section 2
...
---------------------

---------------------
Section 3
...
---------------------

Afterwords

And I want to extract each section by the separator so that I’ll have a result in:

file0:

Section 1
...

file1:

Section 2
...

file2:

Section 3
...

…

Is there a simple way to do this? 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-06-15T18:55:50+00:00Added an answer on June 15, 2026 at 6:55 pm

    [Update] Using chomp and $_ makes this even shorter.

    This should do it:

    If your input record separator is a sequence of 21 -‘s, this is easy with perl -ne:

    perl -ne 'BEGIN{ $/=("-"x21)."\n"; $i=0; } 
      do { open F, ">file".($i++); 
           chomp;
           print F; 
           close F; 
      } if /^Section/' yourfile.txt
    

    should work, and create files file0.. fileN.

    Explanation

    Easier to explain as a stand-alone Perl-script perhaps?

    $/=("-"x21)."\n"; # Set the input-record-separator to "-" x 21 times
    my $i = 0;        # output file number
    
    open IN, "<yourfile.txt" or die "$!";
    
    while (<IN>) {  # Each "record" will be available as $_ 
      do { open F, ">file".($i++); 
           chomp;     # remove the trailing "---..."
           print F;   # write the record to the file
           close F;   #
      } if /^Section/  # do all this only it this is a Section
    }
    

    Perl’s awk lineage was useful here, so let’s show an awk version for comparion:

    awk 'BEGIN{RS="\n-+\n";i=0} 
      /Section/ {chomp; print > "file_"(i++)".txt" 
    }' yourfile.txt
    

    Not too bad compared to the perl version, it’s actually shorter. The $/ in Perl is the RS variable in awk. Awk has an upper hand here: RS may be a regular expression!

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

Sidebar

Related Questions

I have a file that has the following format: 12345 TAB_HERE Name : The
I have a semi xml formatted file that contains line with the following format:
I have a very big text file (few GB) that has the following format:
I have a filename that has the following format: timestamp-username-1 This file is constantly
I have a large text file that contains data in the following format: AUTHOR:
I have a bunch of .txt files that have the following format: |file |
I have a file that is of the following format: text number number A;A;A;A;A;A
I have a file that I need to parse in the following format. (All
If i have a CSV file that is in the following format fd!,sdf,dsfds,dsfd fd!,asdf,dsfds,dsfd
I have a file that contains the following in a csv file; country,region,city,postalCode,metroCode,areaCode I

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.