You can use awk
:
awk '/pattern/{c=5;next} !(c&&c--)' file
Basically: We are decreasing the integer c
on every row of input. We are printing lines when c
is 0
. *(see below) Note: c
will be automatically initialized with 0
by awk upon it's first usage.
When the word pattern
is found, we set c
to 5
which makes c--<=0
false for 5 lines and makes awk not print those lines.
* We could bascially use c--<=0
to check if c
is less or equal than 0
. But when there are many(!) lines between the occurrences of the word pattern
, c
could overflow. To avoid that, oguz ismail suggested to implement the check like this:
!(c&&c--)
This will check if c
is trueish (greater zero) and only then decrement c
. c
will never be less than 0
and therefore not overflow. The inversion of this check !(...)
makes awk
print the correct lines.
Side-note: Normally you would use the word regexp
if you mean a regular expression, not pattern
.