Reputation: 33
From bash, I have a input file with various keywords and sometimes data on following lines. I would like to delete keywords starting from PATTERN1
, until and not including lines starting with PATTERN2
.
Note :
-e
option*
I've tried sed such as
sed -i.bak -e '/*FOO/,/*/d' -e '/*BAR/,/*/d $FILE
but this deletes the *KEEP
line .
**START
*FOO
This wants to be deleted
*KEEP
*BAR
this also wants to be deleted
*KEEP
**END
should become
**START
*KEEP
*KEEP
**END
Thanks in advance for looking at this question
Upvotes: 3
Views: 93
Reputation: 58558
This might work for you (GNU sed):
sed '/^*/h;G;/^*\(FOO\|BAR\)/M!P;d' file
Store each key as it presents itself, in the hold space and append it to the current line. Print the first of the two lines in the pattern space if either does not contain a line beginning *FOO
or *BAR
.
Upvotes: 0
Reputation: 786041
It is easier to do this using awk
:
awk '/^*/{d = 0} /^*(FOO|BAR)/{d = 1} !d' file
*START
*KEEP
*KEEP
**END
If you're using gnu awk
then use -i inplace
to save changes inline:
awk -i inplace '/^*/{d = 0} /\*(FOO|BAR)/{d = 1} !d' file
If not using gnu awk
then use:
awk '/^*/{d = 0} /^*(FOO|BAR)/{d = 1} !d' file > $$.tmp && mv file $$.tmp
Upvotes: 1