Reversing blocks of lines
-
Dear Community,
I’m wondering if there’s a way of reversing the order of blocks of multiple lines in my file.
The weekly reading lists are in reverse order.
Every week has different number of books displayed as Book 1, Book 2, etc. with the items never displaying the exact same title throughout the file. So every book is unique, they are never get repeated.
Sometimes there are additional reading materials, these are either displayed as Newspaper or Notes. Most weeks has neither of them, but sometimes there’s one newspaper, one note or both.
Book titles with the word ‘Notes’ or ‘Newspaper’ can occur.Regarding the above-mentioned parameters, is the task possible somehow, is there any solution?
Sample sequence:
#43 - October 22, 2020
Book 1: Leather Dreams
Book 2: Tried for Slaughter
Book 3: Guitar String Notes
Book 4: The Yellow Snowman
Notes: Books with short chapters
#42 - October 15, 2020
Book 1: Atlantis Ticking
Book 2: Case of the Webbed Falcon
Book 3: Dearest, You Said
Book 4: Sign of the Forgotten Hand
#41 - October 08, 2020
Book 1: The Counterfeit Clock
Book 2: Tomb of Spades
Book 3: Built for Ruin
Book 4: Son of a Rogue
Book 5: Cheat the Mirage
Book 6: He Stole My Knight
#40 - October 01, 2020
Book 1: Blue Moon
Book 2: Newspaper With a Face
Book 3: Scars of Spades
Book 4: The Accidental Wife
Book 5: Clue of the Whispering Tuba
#39 - September 24, 2020
Book 1: The Dynasty of Avalon
Book 2: The Viper in the Mist
Book 3: Secret of the Spanish Stranger
Book 4: The Visitor in the Lake
#38 - September 17, 2020
Book 1: The Specter in the Veil
Book 2: Sign of the Wooden Amulet
Book 3: Ceasefire of the Senses
Book 4: The Queen of the Empire
Newspaper: Die Süddeutsche Zeitung
Notes: Secret-identity novels
#37 - September 10, 2020
Book 1: Case of the Sacred Bulldog
Book 2: Mystery of the Quiet Cricketer
Book 3: The Screaming Maple
Book 4: The Hand of the Titan
Book 5: Trap the Storm
Newspaper: Le Monde
Notes: Books with plot twists
What I’d like to achieve:
#37 - September 10, 2020
Book 1: Case of the Sacred Bulldog
Book 2: Mystery of the Quiet Cricketer
Book 3: The Screaming Maple
Book 4: The Hand of the Titan
Book 5: Trap the Storm
Newspaper: Le Monde
Notes: Books with plot twists
#38 - September 17, 2020
Book 1: The Specter in the Veil
Book 2: Sign of the Wooden Amulet
Book 3: Ceasefire of the Senses
Book 4: The Queen of the Empire
Newspaper: Die Süddeutsche Zeitung
Notes: Secret-identity novels
#39 - September 24, 2020
Book 1: The Dynasty of Avalon
Book 2: The Viper in the Mist
Book 3: Secret of the Spanish Stranger
Book 4: The Visitor in the Lake
#40 - October 01, 2020
Book 1: Blue Moon
Book 2: Binary Leviathan
Book 3: Scars of Spades
Book 4: The Accidental Wife
Book 5: Clue of the Whispering Tuba
#41 - October 08, 2020
Book 1: The Counterfeit Clock
Book 2: Tomb of Spades
Book 3: Built for Ruin
Book 4: Son of a Rogue
Book 5: Cheat the Mirage
Book 6: He Stole My Knight
#42 - October 15, 2020
Book 1: Atlantis Ticking
Book 2: Case of the Webbed Falcon
Book 3: Dearest, You Said
Book 4: Sign of the Forgotten Hand
#43 - October 22, 2020
Book 1: Leather Dreams
Book 2: Tried for Slaughter
Book 3: The Glass Birds
Book 4: The Yellow Snowman
Notes: Books with short chapters
Thank you so much and have a nice day,
Viktoria -
@Viktoria-Ontapado said in Reversing blocks of lines:
I’m wondering if there’s a way of reversing the order of blocks of multiple lines in my file.
I’m not on my PC currently to test my solution but as you have the book number which is what you want to sort on, all you need to do is remove all the CR and LF at the end of lines which are not followed by the # and number.
Once the block becomes one line sort would be easy. Of course to put the lines back as they were will entail having a delimiter replace those CR and LF we removed earlier. This would need to be 1 or 2 characters, such as
&&
that would never appear in the text normally.So steps are:
- Replace all CR and LF (regex defines this as
\R
) with a delimiter (perhaps&&
) when not followed by the#\d+
. - Sort using the
#\d+
, this would be a lexicographically ascending sort. - Replace the
&&
with\r\n
.
Job should be complete.
Terry
- Replace all CR and LF (regex defines this as
-
So I’ll step-by-step the Terry R solution for you.
- find:
\R(?!\#\d+)
repl:&&
to obtain:
#43 - October 22, 2020&&Book 1: Leather Dreams&&Book 2: Tried for Slaughter&&Book 3: Guitar String Notes&&Book 4: The Yellow Snowman&&Notes: Books with short chapters #42 - October 15, 2020&&Book 1: Atlantis Ticking&&Book 2: Case of the Webbed Falcon&&Book 3: Dearest, You Said&&Book 4: Sign of the Forgotten Hand #41 - October 08, 2020&&Book 1: The Counterfeit Clock&&Book 2: Tomb of Spades&&Book 3: Built for Ruin&&Book 4: Son of a Rogue&&Book 5: Cheat the Mirage&&Book 6: He Stole My Knight #40 - October 01, 2020&&Book 1: Blue Moon&&Book 2: Newspaper With a Face&&Book 3: Scars of Spades&&Book 4: The Accidental Wife&&Book 5: Clue of the Whispering Tuba #39 - September 24, 2020&&Book 1: The Dynasty of Avalon&&Book 2: The Viper in the Mist&&Book 3: Secret of the Spanish Stranger&&Book 4: The Visitor in the Lake #38 - September 17, 2020&&Book 1: The Specter in the Veil&&Book 2: Sign of the Wooden Amulet&&Book 3: Ceasefire of the Senses&&Book 4: The Queen of the Empire&&Newspaper: Die Süddeutsche Zeitung&&Notes: Secret-identity novels #37 - September 10, 2020&&Book 1: Case of the Sacred Bulldog&&Book 2: Mystery of the Quiet Cricketer&&Book 3: The Screaming Maple&&Book 4: The Hand of the Titan&&Book 5: Trap the Storm&&Newspaper: Le Monde&&Notes: Books with plot twists
- sort to obtain:
#37 - September 10, 2020&&Book 1: Case of the Sacred Bulldog&&Book 2: Mystery of the Quiet Cricketer&&Book 3: The Screaming Maple&&Book 4: The Hand of the Titan&&Book 5: Trap the Storm&&Newspaper: Le Monde&&Notes: Books with plot twists #38 - September 17, 2020&&Book 1: The Specter in the Veil&&Book 2: Sign of the Wooden Amulet&&Book 3: Ceasefire of the Senses&&Book 4: The Queen of the Empire&&Newspaper: Die Süddeutsche Zeitung&&Notes: Secret-identity novels #39 - September 24, 2020&&Book 1: The Dynasty of Avalon&&Book 2: The Viper in the Mist&&Book 3: Secret of the Spanish Stranger&&Book 4: The Visitor in the Lake #40 - October 01, 2020&&Book 1: Blue Moon&&Book 2: Newspaper With a Face&&Book 3: Scars of Spades&&Book 4: The Accidental Wife&&Book 5: Clue of the Whispering Tuba #41 - October 08, 2020&&Book 1: The Counterfeit Clock&&Book 2: Tomb of Spades&&Book 3: Built for Ruin&&Book 4: Son of a Rogue&&Book 5: Cheat the Mirage&&Book 6: He Stole My Knight #42 - October 15, 2020&&Book 1: Atlantis Ticking&&Book 2: Case of the Webbed Falcon&&Book 3: Dearest, You Said&&Book 4: Sign of the Forgotten Hand #43 - October 22, 2020&&Book 1: Leather Dreams&&Book 2: Tried for Slaughter&&Book 3: Guitar String Notes&&Book 4: The Yellow Snowman&&Notes: Books with short chapters
- replace
&&
(as written by Terry) to obtain:
#37 - September 10, 2020 Book 1: Case of the Sacred Bulldog Book 2: Mystery of the Quiet Cricketer Book 3: The Screaming Maple Book 4: The Hand of the Titan Book 5: Trap the Storm Newspaper: Le Monde Notes: Books with plot twists #38 - September 17, 2020 Book 1: The Specter in the Veil Book 2: Sign of the Wooden Amulet Book 3: Ceasefire of the Senses Book 4: The Queen of the Empire Newspaper: Die Süddeutsche Zeitung Notes: Secret-identity novels #39 - September 24, 2020 Book 1: The Dynasty of Avalon Book 2: The Viper in the Mist Book 3: Secret of the Spanish Stranger Book 4: The Visitor in the Lake #40 - October 01, 2020 Book 1: Blue Moon Book 2: Newspaper With a Face Book 3: Scars of Spades Book 4: The Accidental Wife Book 5: Clue of the Whispering Tuba #41 - October 08, 2020 Book 1: The Counterfeit Clock Book 2: Tomb of Spades Book 3: Built for Ruin Book 4: Son of a Rogue Book 5: Cheat the Mirage Book 6: He Stole My Knight #42 - October 15, 2020 Book 1: Atlantis Ticking Book 2: Case of the Webbed Falcon Book 3: Dearest, You Said Book 4: Sign of the Forgotten Hand #43 - October 22, 2020 Book 1: Leather Dreams Book 2: Tried for Slaughter Book 3: Guitar String Notes Book 4: The Yellow Snowman Notes: Books with short chapters
- find:
-
Awesome idea, I didn’t think of that!
Thank you so much for the solution and for the step-by-step guide as well. Much obliged.Viktória