1

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.

  • 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
1

Try this:

sed '/action X/{ N; N; d }'
0

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

awk '/action X/ { getline; getline; getline } 1' 

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.