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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:12:55+00:00 2026-06-05T02:12:55+00:00

How can I revert the order of some blocks of text using only bash

  • 0

How can I revert the order of some blocks of text using only bash commands like sed and cat? What I want is something like tac, but instead of operating line by line, it would operate block by block. Example:

From

/Section 3/
Rabbits
Dogs
Cats

/Section 2/
Eagles
Mice

/Section 1/
Dogs
Rabbits
Lemmings

To

/Section 1/
Dogs
Rabbits
Lemmings

/Section 2/
Eagles
Mice

/Section 3/
Rabbits
Dogs
Cats

In some files, the beginning of the block is marked by a slash, as in the example above. In others, the blocks are marked only by the existence of one or more blank lines between them.

  • 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-05T02:12:57+00:00Added an answer on June 5, 2026 at 2:12 am

    In emacs, you can use the sort-paragraphs command:

    Ctrl-xhMeta-xsort-paragraphsEnter


    In vim: https://superuser.com/questions/365094/sort-file-per-paragraph-in-vim


    Use the basic unix tools:

    awk -F'\n' -vRS='' -vOFS=',' '{$1=$1}1' input.txt |
        sort |
            tr ',' '\n' |
                sed 's@^/@\n/@'
    

    I use awk to transform the data to a csv, then sort the csv, at last I transform the csv back to list style.


    result:

    /Section 1/
    Dogs
    Rabbits
    Lemmings
    
    /Section 2/
    Eagles
    Mice
    
    /Section 3/
    Rabbits
    Dogs
    Cats
    

    Edit: Sorry, I didn’t look at your question very carefully. You can change the sort command to tac to reverse the order.

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

Sidebar

Related Questions

How can i revert a commit having the following situation? Revision 3, Commit Message
How can I revert back to the default function that python uses if there
How can I undo or revert an user entry on a QTableView popuplated by
Can I order my users in the database, so I don't have to say
Can I authenticate with just Google account username and password instead of using OAuth?
Note that the order can go either way (erase first then push back, just
I'm using jeegoo context menu jquery plugin which overrides the arrow keys in order
After submitting an app, is there a snapshot I can revert to? I made
I'd like to change my jQM page's URL dynamically (when the user does some
All my table data comes in a sorted order. I am using table sorter

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.