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
If you can only use grep:
grep -A100000 test1 file.txt | grep -B100000 test2 > new.txt
grep -A
and then a number gets the lines after the matching string, and grep -B
gets the lines before the matching string. The number, 100000 in this case, has to be large enough to include all lines before and after.
If you don't want to include test1 and test2, then you can remove them afterwards by grep -v
, which prints everything except the matching line(s):
egrep -v "test1|test2" new.txt > newer.txt
or everything in one line:
grep -A100000 test1 file.txt | grep -B100000 test2 | egrep -v "test1|test2" > new.txt