How to search & delete inclusively between two lines?

Hi all, I was wondering if anyone would know how to search & delete inclusively between two lines, please:

Important:

  • There are multiple }; lines. I'm curious how to delete the correct one only.
  • Line numbers may vary each time this script is run.

For example, I'd like to delete only the entire entry called deletethisentry.com

zone "localhost" {
        type master;
        notify no;
        file "db.myzone";
};

zone "deletethisentry.com" {
        type master;
        notify no;
        file "db.myzone";
};

zone "keepthisentry.com" {
        type master;
        notify no;
        file "db.myzone";
};

Thanks so much!
CG

Any attempts / ideas / thoughts from your side?

Unfortunately this attempt doesn't work, because it incorrectly deletes the rest of the file:

#Remove deletethisentry.com
sed -n '/deletethisentry.com/{:a;N;/"};"/!ba;N;s/.*\n/ /};p' /var/named/zones.conf

The problem I have above, is that it should delete only to the next "};" but it instead incorrectly deletes up to the last one.

awk OK?

awk '/delete/, /^}/ {next} 1' file
zone "localhost" {
type master;
notify no;
file "db.myzone";
};


zone "keepthisentry.com" {
type master;
notify no;
file "db.myzone";
};
1 Like
sed '/delete/,/^$/d' file
1 Like
awk '$1=="zone" { del=($2~/deletethisentry.com/) } !del' /var/named/zones.conf

If Field#1 is "zone" then set del according to field#2 containing "deletethisentry.com"; print if del is 0.

2 Likes

Thanks everyone above for your help!

And MadeInGermany, thank you so much!!! What you suggested works great!!! I never thought this could be achieved with 1 simple line of awk!!

Thanks again!!
CG