Skip to main content
3 of 5
deleted 2 characters in body
jubilatious1
  • 3.9k
  • 10
  • 21

Using Raku (formerly known as Perl_6)

~$ raku -e 'my @a; for slurp() { @a = .comb(/^^ Chapter .*? <?before \nChapter | $ > /) }; @a.grep(/quench/).put;' file 

Certain someone will post a Perl answer, but here's an answer written in Raku (a.k.a. Perl6). Raku provides high-level support for Unicode, built-in.

Briefly, the file is slurped in, and combed through to locate matching records (Chapters). Then in the final statement grep is used to only return matching records (Chapters). Sample Input is same as provided by OP.

Sample Output:

Chapter: 1 One: Birds and Trees Birds are beautiful and trees are amazing and they are dependent on each other. Birds most of the time choose to make their nests on trees since trees provide more stability. One day the bird sat on a tree and said; Bird: Oh my I'm so tired from all the flying, I should take a rest Tree: Mr Bird, you seem tired, perhaps you should take some rest, and here are some fruits to quench your thirst. Bird: Oh thank you very much! Reference: Chapter 1: birds and trees 

Add calls to trim, trim-leading or trim-trailing in the final statement to remove surrounding whitespace, as desired.

https://raku.org

jubilatious1
  • 3.9k
  • 10
  • 21