Skip to main content
1 of 4

Printing a specific section everytime search results are matched

I have a pretty basic text file that has stuff like Chapters, Dialogues and References.

This is what it looks like

 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 Chapter: 2 Two: Trees and Fruits Fruits are very delicious to eat and they are mostly found in trees. Fruits contain essential vitamins, minerals and loads of good fibers. Reference: Chapter 2: trees and fruits 

These are the contents on the txt file. Now say I was searching for quench, I was thinking that it would start from Chapter number upto Reference. So I tried with grep;

$ grep -A 5 -B 5 'quench' file.txt 

However, this does not produce the desired output. I was expecting something like this;

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 

I was wondering if this was possible to achieve through sed or awk.