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.
In
emacs, you can use thesort-paragraphscommand:Ctrl-xhMeta-xsort-paragraphsEnter
In
vim: https://superuser.com/questions/365094/sort-file-per-paragraph-in-vimUse the basic unix tools:
I use
awkto transform the data to acsv, thensortthecsv, at last I transform thecsvback to list style.result:
Edit: Sorry, I didn’t look at your question very carefully. You can change the
sortcommand totacto reverse the order.