Skip to main content
added 19 characters in body; edited tags
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

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

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

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

added 309 characters in body
Source Link

And also, searching for the word 'vitamin' would print out;

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 

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

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

And also, searching for the word 'vitamin' would print out;

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 

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

added 51 characters in body
Source Link

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.

P.S: Each new line is an actual new line though

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.

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.

P.S: Each new line is an actual new line though

Source Link
Loading