Lets say I have the following content in a text file
...
abc
123
qwerty
line0
...
line3
line10
max
....
How to I grep
for abc
and capture all lines after the match and stop at line that contains specific text, in this example max
?
Lets say I have the following content in a text file
...
abc
123
qwerty
line0
...
line3
line10
max
....
How to I grep
for abc
and capture all lines after the match and stop at line that contains specific text, in this example max
?
You can use sed
:
sed '/abc/,/max/!d'
!
is negationd
means delete/addr1/,/addr2/
is an "address range"Similarly in Perl:
perl -ne 'print if /abc/ .. /max/'
Even simpler than sed
and my beloved Perl is awk
.
awk '/abc/,/max/' filename
Line range addressing is simple in sed
/awk
, but if you really need to use grep
, this will work:
grep -Poz 'abc[^\0]*max' file
The -z
flag makes grep
treat the input as null-byte terminated lines, so assuming your input doesn't contain any \0
bytes, the above PCRE will select everything between abc
and max
.