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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:30:53+00:00 2026-06-14T19:30:53+00:00

In the following example, there are 3 elements that have to be sorted: [aaa]

  • 0

In the following example, there are 3 elements that have to be sorted:

  1. “[aaa]” and the 4 lines (always 4) below it form a single unit.
  2. “[kkk]” and the 4 lines (always 4) below it form a single unit.
  3. “[zzz]” and the 4 lines (always 4) below it form a single unit.

Only groups of lines following this pattern should be sorted; anything before “[aaa]” and after the 4th line of “[zzz]” must be left intact.

from:

This sentence and everything above it should not be sorted.

[zzz]
some
random
text
here
[aaa]
bla
blo
blu
bli
[kkk]
1
44
2
88

And neither should this one and everything below it.

to:

This sentence and everything above it should not be sorted.

[aaa]
bla
blo
blu
bli
[kkk]
1
44
2
88
[zzz]
some
random
text
here

And neither should this one and everything below it.
  • 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-14T19:30:54+00:00Added an answer on June 14, 2026 at 7:30 pm

    Maybe not the fastest 🙂 [1] but it will do what you want, I believe:

    for line in $(grep -n '^\[.*\]$' sections.txt |
                  sort -k2 -t: |
                  cut -f1 -d:); do
      tail -n +$line sections.txt | head -n 5
    done
    

    Here’s a better one:

    for pos in $(grep -b '^\[.*\]$' sections.txt |
                 sort -k2 -t: |
                 cut -f1 -d:); do
      tail -c +$((pos+1)) sections.txt | head -n 5
    done
    

    [1] The first one is something like O(N^2) in the number of lines in the file, since it has to read all the way to the section for each section. The second one, which can seek immediately to the right character position, should be closer to O(N log N).

    [2] This takes you at your word that there are always exactly five lines in each section (header plus four following), hence head -n 5. However, it would be really easy to replace that with something which read up to but not including the next line starting with a ‘[‘, in case that ever turns out to be necessary.


    Preserving start and end requires a bit more work:

    # Find all the sections
    mapfile indices < <(grep -b '^\[.*\]$' sections.txt)
    # Output the prefix
    head -c+${indices[0]%%:*} sections.txt
    # Output sections, as above
    for pos in $(printf %s "${indices[@]}" |
                 sort -k2 -t: |
                 cut -f1 -d:); do
      tail -c +$((pos+1)) sections.txt | head -n 5
    done
    # Output the suffix
    tail -c+$((1+${indices[-1]%%:*})) sections.txt | tail -n+6
    

    You might want to make a function out of that, or a script file, changing sections.txt to $1 throughout.

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

Sidebar

Related Questions

How can I remove the unwanted whitespace in following example? Note 1. that there
I have a server-generated html like: <ul> <li><!-- few nested elements that form a
I have the following problem. I have a set of elements that I can
The official documentation states the following: . But I have noticed that there are
I have the following lines in a file. Here is an example of one
Say, there is a following example: class Super { public int i = 3;
I'm trying to implement a delegate class following Herb Sutter's Example . There is
Say I have the following code: <div onclick='location.href=http://www.example.com/'> <a href='#' onclick='alert(blah)'>click</a> </div> Is there
I do not understand the following example, let's say I have these functions: #
I had a following interview question. There is an array of nxn elements. The

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.