Advanced grep unix

Sachin Chourasiya picture Sachin Chourasiya · Nov 6, 2009 · Viewed 26.7k times · Source

Usually grep command is used to display the line contaning the specified pattern. Is there any way to display n lines before and after the line which contains the specified pattern?

Can this will be achieved using awk?

Answer

pavium picture pavium · Nov 6, 2009

Yes, use

grep -B num1 -A num2 

to include num1 lines of context before the match, and num2 lines of context after the match.

EDIT:

Seems the OP is using AIX. This has a different set of options which doesn't include -B and -A

this link describes grep on AIX 4.3 (it doesn't look promising)

Matt's perl script might be a better solution.