How to use tail -f with grep to show surrounding lines

greptail

I would like to see the output in a logfile greped by only one domain but also the following two lines.

Example:

tail -f /var/log/apache2/modsec_audit.log |grep mydomain.de

this shows all lines, that contain "mydomain.de" but the important information is in the line below the line, where the domain is included

Best Answer

grep has extra options to define how many lines before and after the result:

  • -A (after)
  • -B (before)
  • -C (context [before + after])

So in your case you need -A:

YOUR_COMMAND |grep -A NUMBER YOURDOMAIN

the above command prints NUMBER of lines after YOURDOMAIN in file.

Related Question