Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

up vote 9 down vote accepted

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.

share|improve this answer
    
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 '13 at 8:14
    
If you're on GNU/Linux. This is one of those cases where "GNU/Linux" is not only correct, it's also highly adequate ;-) –  njsg Feb 4 '13 at 8:46
    
I agree, But nowadays some of OSes uses some of GNU extension in a lot of appl. gcc or ... –  Mohsen Pahlevanzadeh Feb 4 '13 at 9:19
add comment

Try passing number of context lines to grep.

| grep -C 5

will print 5 lines before and after the match.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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