For searching logs, you will often want to see the content surrounding the string you are searching for with grep.

Here is a quick reminder of the syntax to show the extra content around your search using GREP.

Get Lines Before and After Search String

Type your normal grep command in with options, string, and the file(s) you want to search through, then add the following syntax on the end.

Use the flag -C, followed by the number of lines before and after, separated by the pound sign.


grep -ri "mystring" mylogfile.log -C 50 # 50

It’s an easy syntax, but I always forget so I thought I would record it here. It has helped me troll log files for large chunks of text or to get complete error messages/dumps from my scripts.