Many good answers have been given to this question so far, but I still miss one with awk
not using getline
. Since, in general, it is not necessary to use getline
, I would go for:
awk ' f && NR==f+1; /blah/ {f=NR}' file #all matches after "blah"
or
awk '/blah/ {f=NR} f && NR==f+1' file #matches after "blah" not being also "blah"
The logic always consists in storing the line where "blah" is found and then printing those lines that are one line after.
Test
Sample file:
$ cat a
0
blah1
1
2
3
blah2
4
5
6
blah3
blah4
7
Get all the lines after "blah". This prints another "blah" if it appears after the first one.
$ awk 'f&&NR==f+1; /blah/ {f=NR}' a
1
4
blah4
7
Get all the lines after "blah" if they do not contain "blah" themselves.
$ awk '/blah/ {f=NR} f && NR==f+1' a
1
4
7