25

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

2 Answers 2

35

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.

1
  • Please note, however, that this is a GNU extension. If you're on Linux, your grep definitely has this option. If you're not, you better check.
    – Leonid
    Feb 4, 2013 at 8:14
9

Try passing number of context lines to grep.

| grep -C 5

will print 5 lines before and after the match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.