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

Asked by Julianloui on 2013-12-22

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:
2013-12-22
Last query:
2013-12-22
Last reply:
2013-12-22
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.

Julianloui (julianloui) said : #2

Thomas,

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

Julianloui

Julianloui (julianloui) said : #3

Thanks Thomas Krüger, that solved my question.