How do I list top or bottom 10 lines from the line that matched the word 'error' in a file.
I'm using grep 'error' /var/log/logfile.log > errors
to print and save the lines that matched the word 'error' in to the file called 'errors'. How could I change this to suite my requirement?. Anybody has any idea?
Displaying lines before/after/around the match using grep -A, -B and -C
-A is the option which prints the specified N lines after the match as shown below.
-B is the option which prints the specified N lines before the match.
-C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match.
Source: http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/ (Also I would recommend you to read the full blog post)
so, the command should be like
grep -C 10 'error'