Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

Try this:

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .