Need 4 line after grep

I'm greping for a word in a file however I need to display 4 lines after the grepped output line. I'm using Solaris so the -A option wont work for me. Can someone help me acheive this.

example:

grep -i "Hello" output.txt

grep doesn't have any recall of previous lines and no way to connect them with 'if this line, do this for the next line' kind of logic. You'll need to use something else.

easy enough with awk. whenever the line contains pattern, L is set to 5. Every line (including matching ones) decrements L, and lines are only printed when L>0, so it prints the pattern line and four more.

awk '/pattern/ { L=5; } ((L--)>0)' filename
1 Like

Sed way..

sed -n '/pattern/{N;N;N;N;p}' inputfile

One more way,

 
perl -0pe '/pattern((.*\n){4})/;$_=$1' input.