How to print lines before and after a match found by grep in a document

Asked by Julianloui

Frequently I feel a need to display a few lines before and after a match found by grep in Linux. I'll appreciate anyone's help in this matter.

Julianloui

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gnome-terminal Edit question
Assignee:
No assignee Edit question
Solved by:
Thomas Krüger
Solved:
Last query:
Last reply:
Revision history for this message
Best Thomas Krüger (thkrueger) said :
#1

Add the parameters -A<num> and -B<num>, where <num> is the number of lines. e.g.:
fgrep -A3 -B1 error mail.log
will print 1 line before and 3 lines after a line with "error" in mail.log.

Revision history for this message
Julianloui (julianloui) said :
#2

Thomas,

Thanks very much. Your suggestion works beautifully. One never learns enough about Linux.

Julianloui

Revision history for this message
Julianloui (julianloui) said :
#3

Thanks Thomas Krüger, that solved my question.