Linux: How do I grep text from one string to another

Linux: How do I grep text from one string to another … here is a solution to the problem.

Linux: How do I grep text from one string to another

How do I get text using the grep command txt located between two strings?

For example:

<--string 1-->
the text i need
<--string 2-->

The “text I need”

between the two labels is dynamic, so I need a command to output the text from

“” to “”

Solution

This might work for you:

grep -A2 "<--string 1-->" file | grep -v "<--string 1-->\|<--string 2-->"

or

grep -A1 "<--string 1-->" file | grep -v "<--string 1-->"

Or in a single process:

sed '/<--string 1-->/,/<--string 2-->/!d;//d' file

Or:

awk '/<--string 2-->/{p=0}; p;/<--string 1-->/{p=1}' file

Related Problems and Solutions