Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete multiline pattern containing another pattern

Tags:

grep

sed

awk

I would to like to delete all instances of a pattern which contain another specified pattern (conveniently on a new line):

MID:

Sample Input:

header
BEGIN:
1abc
7wurw
END:
BEGIN:
22xyz
MID:
34utov
END:

Desired Output:

header
BEGIN:
1abc
7wurw
END:

I'm looking for possible one liners. Any help would be appreciated.

like image 215
okapiho Avatar asked Jun 25 '26 20:06

okapiho


1 Answers

Using GNU sed:

sed -e :a -e '/^BEGIN:/,/^END:/ { /END:/!{$!{N;ba};};/MID:/d;}' inputfile

For your input, it'd return:

header
BEGIN:
1abc
7wurw
END:
like image 105
devnull Avatar answered Jun 28 '26 19:06

devnull



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!