How to remove a block of text, but only if it contains a specified string

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to remove a block of text, but only if it contains a specified string

laubrino
Hello

I'm parsing logs. And I would need to conditionally remove a block of text. Something like
sed '/A/,B/d'
but only if the block contains line C.

Input lines are

A
C
B
A
D
B

and I expect this result

A
D
B

Is that possibe in shell script?

Thank you
Reply | Threaded
Open this post in threaded view
|

Re: How to remove a block of text, but only if it contains a specified string

Guru
Administrator
sed "/A/{N;N;/C.*B/d;}" file
Reply | Threaded
Open this post in threaded view
|

Re: How to remove a block of text, but only if it contains a specified string

laubrino
Thank you very much, for your quick answer. Unfortunatelly it doesn't work for me. Maybe the example was to simple and I'm not able to adjust your command to work with my real cause. I'll give you more complex example:

some text here
hello line begins here
aaaaa
bbbb ZZZ bbbb
ccccccc
hello line ends here
some text
hello line begins here
aaaaa
bbbb 111 bbbb
ccccccc
hello line ends here
some text


So now I would like to remove the block, which starts with "line begins" and ends with "line ends", but only if it contains ZZZ somewhere in between.

The result would be:

some text here
some text
hello line begins here
aaaaa
bbbb 111 bbbb
ccccccc
hello line ends here
some text



It it was too general, you could assume the the ZZZ string is always on the second line in the block (right after "line begins").