Example:
a43 test1 abc cvb bnm test2 kfo
I need all lines between test1 and test2. Normal grep does not work in this case. Do you have any propositions?
How do I extract text between two words ( <PRE> and </PRE> ) in unix or linux using grep command? Let us see how use the grep command or egrep command to extract data between two words or strings. I also suggest exploring sed/awk/perl commands to extract text between two words on your Linux or Unix machine.
You can use option -A (after) and -B (before) in your grep command.
Print from
test1
totest2
(Trigger lines included)
awk '/test1/{f=1} /test2/{f=0;print} f' awk '/test1/{f=1} f; /test2/{f=0}' awk '/test1/,/test2/'
test1 abc cvb bnm test2
Prints data between
test1
totest2
(Trigger lines excluded)
awk '/test1/{f=1;next} /test2/{f=0} f' awk '/test2/{f=0} f; /test1/{f=1}'
abc cvb bnm
You could use sed
:
sed -n '/test1/,/test2/p' filename
In order to exclude the lines containing test1
and test2
, say:
sed -n '/test1/,/test2/{/test1/b;/test2/b;p}' filename
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With