Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Consider a log file which looks like this:

action A
detail 1
detail 2
detail 3
action X
detail 1
detail 2
action B
detail 1
action X
detail 1
detail 2
action C

Lines with "action [ABC]" may have various number of following "detail"-lines.

Lines with "action X" always have 2 following "detail"-lines.

I am not interested in event X so I would like to filter out all lines with "action X" and the following two lines.

I am aware of the -v flag, just not how to express the "and the following 2 lines" part.

share|improve this question
    
Currently grep does not support context switches to be used with the -v switch, although there have been some suggestions to add it. I think sed would be the best alternative for you. –  Thor Feb 14 '13 at 8:33
add comment

2 Answers

Try this:

sed '/action X/{ N; N; d }'
share|improve this answer
add comment

Here's an awk alternative that does the same as Nicole's sed answer:

awk '/action X/ { getline; getline; getline } 1' 
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.