rdo
rdo

Reputation: 3982

How to remove a text block from a file with bash

I have file of bind config with following config:

zone "domain1.com" {
        type master;
        file "masters/domain1.com";
        allow-transfer {
                dnscontroller_acl;
        };
};


zone "domain2.com" {
        type master;
        file "masters/domain2.com";
        allow-transfer {
                dnscontroller_acl;
        };
};

zone "domain3.com" {
        type master;
        file "masters/domain3.com";
        allow-transfer {
                dnscontroller_acl;
        };
};

zone "domain4.com" {
        type master;
        file "masters/domain4.com";
        allow-transfer {
                dnscontroller_acl;
        };
};

How to remove zone config (start from zone filename and end of };) from file with help of bash?

Upvotes: 2

Views: 4171

Answers (2)

Thor
Thor

Reputation: 47099

If the file is well ordered, you could use awk with automatic record- and field separation:

awk '
  BEGIN { RS = ORS = "\n\n"; FS="\n" } 
  $1 !~ /domain3/
' file

Removes zone where the first line contains "domain3".

Upvotes: 1

choroba
choroba

Reputation: 241908

You can use sed to remove the config for a given zone:

sed '/^zone "domain4.com" {$/,/^};/d' file

If you want a script that can take a zone as an argument, just add the she-bang and the argument:

#!/bin/bash
sed '/^zone "'"$1"'" {$/,/^};/d' file

Upvotes: 7

Related Questions