If you have GNU grep, then it is possible. From man grep
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing -- between contiguous groups of
matches.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing -- between contiguous groups of
matches.
There is a very simple way of doing this. Lets say you have a file test.txt having 'view' in some line. Say, you wish to get 5 lines above and below the line containing 'view' and output to a file say test_one.txt use the following: