Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux: How can i grep text from string to string

Tags:

linux

grep

bash

How can I get text using grep command txt that seats between two strings?

for example:

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

the "the text i need" between the two tags is dynamic, therefor i need a command that will output text from "<--string 1-->" to "<--string 2-->"

like image 866
Crazy_Bash Avatar asked Oct 23 '25 19:10

Crazy_Bash


1 Answers

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
like image 114
potong Avatar answered Oct 26 '25 10:10

potong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!