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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:15:58+00:00 2026-05-31T19:15:58+00:00

I have a file containing records delimited by the pattern /#matchee/. These records are

  • 0

I have a file containing records delimited by the pattern /#matchee/. These records are of varying lengths …say 45 – 75 lines. They need to ALL be 45 lines and still maintain the record delimiter. Records can be from different departments, department name is on line 2 following a blank line. So record delimiter could be thought of as simply /^#matchee/ or /^matchee/ followed by \n. There is a Deluxe edition of this problem and a Walmart edition …

DELUXE EDITION

Pull each record by pattern range so I can sort records by department. Eg., with sed

sed -n '/^DEPARTMENT NAME/,/^#matchee/{p;}' mess-o-records.txt

Then, Print only the first 45 lines of each record in the file to conform to
the 45 line constraint.

Finally, make sure the result still has the record delimiter on line 45.

WALMART EDITION

Same as above, but instead of using a range, just use the record delimiter.

STATUS

My attempt at this might clarify what I’m trying to do.

sed -n -e '/^DEPARTMENT-A/,/^#matchee/{p;}' -e '45q' -e '$s/.*/#matchee/' mess-o-records.txt

This doesn’t work, of course, because sed is operating on the entire file at each command.
I need it to operate on each range match not the whole file.

SAMPLE INPUT – 80 Lines ( truncated for space )

<blank line>
DEPARTMENT-A
Office space 206
Anonymous, MI 99999

Harold O Nonymous
Buckminster Abbey
Anonymous, MI 99999

item A     Socket B     45454545
item B     Gizmo Z      76767676
<too many lines here>
<way too many lines here>  


#matchee

SAMPLE OUTPUT – now only 45 lines

<blank line>
DEPARTMENT-A
Office space 206
Anonymous, MI 99999

Harold O Nonymous
Buckminster Abbey
Anonymous, MI 99999

item A     Socket B     45454545
item B     Gizmo Z      76767676
<Record now equals exactly 45 lines>  
<yet record delimiter is maintained>

#matchee

CLARIFICATION UPDATE

I will never need more than the first 40 lines if this makes things easier. Maybe the process would be:

  • Match pattern(s)
  • Print first 40 lines.
  • Pad to appropriate length. Eg., 45 lines.
  • Tack delimiter back on. Eg., #matchee

I think this would be more flexible — Ie., can handle record shorter than 45 lines.

Here’s a riff based on @Borodin’s Perl example below:

my $count = 0;
$/ = "#matchee";    

while (<>) {
    if (/^REDUNDANCY.*DEPT/) {
        print;
        $count = 0;
    }   
    else {
        print if $count++ < 40; 
        print "\r\n" x 5; 
        print "#matchee\r\n";
    }   
}

This add 5 newlines to each record + the delimiting pattern /#matchee/. So it’s wrong — but it illustrates what I want.

Print 40 lines based on department — pad — tack delimiter back on.

  • 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-31T19:15:59+00:00Added an answer on May 31, 2026 at 7:15 pm

    I think I understand what you want. Not sure about the bit about pull each record by pattern range. Is #matchee always followed by a blank line and then the department line? So in fact record number 2?

    This Perl fragment does what I understand you need.

    If you prefer you can put the input file on the command line and drop the open call. Then the loop would have to be while (<>) { ... }.

    Let us know if this is right so far, and what more you need from it.

    use strict;
    use warnings;
    
    open my $fh, '<', 'mess-o-records.txt' or die $!;
    
    my $count = 0;
    
    while (<$fh>) {
      if (/^#matchee/) {
        print;
        $count = 0;
      }
      else {
        print if $count++ < 45;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file containing a couple thousands words on individual lines. I need
I have a text file containing a number of records. Each record is stored
We have an Excel file with a worksheet containing people records. 1. Phone Number
I have a txt file containing many records, each record has 5 rows and
we have a text file containing binary values. say, we have a file file.txt,it
i have a file containing list paths and other related information of all the
I have a text file containing some records. Each record is splitted in 4
I have a .csv file containing over 70 million lines of which each line
I have a large text file of records, each delimited by a newline. Each
I have a database with 1000 records containing a file name and a file

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.