Issue
I have a file
###
A
B
C
@@@
###
D
E
F
@@@
###
G
H
I
@@@
I want to remove all lines between ### and @@@ (inclusive) if it contains A
expected result
###
D
E
F
@@@
###
G
H
I
@@@
I have tried
sed "/###/,/@@@/d"
but that is removing everything.
If we can have multiple text patterns like remove everything between ### and @@@ if it contains either 'A' or 'D' would be even helpful.
Thanks.
Solution
You can use
sed '/###/{:a;N;/@@@/!ba;/A/d}' file
Details:
/###/
- looks for a###
and once it is found,:a
- seta
label at the current locationN
- reads the next line appending it to pattern space/@@@/!ba
- if there is@@@
stop processing block, else, go toa
label/A/d
- ifA
is inside the block matched, delete it.
See an online test:
#!/bin/bash
text=$(echo -e '###\nA\nB\nC\n@@@\n###\nD\nE\nF\n@@@\n###\nG\nH\nI\n@@@')
sed '/###/{:a;N;/@@@/!ba;/A/d}' <<< "$text"
Output:
###
D
E
F
@@@
###
G
H
I
@@@
Answered By - Wiktor Stribiżew Answer Checked By - Willingham (WPSolving Volunteer)