Satish
Satish

Reputation: 17407

Regex extract multiple line from file

How can I extract two fields from a given file "named.conf"? I want the fields 'zone' and 'file'.

zone "example.com" IN {
        type master;
        file "db.example.com";
        allow-query { any; };
        allow-update { none; };
        allow-transfer { 10.101.100.2; };
};

Upvotes: 1

Views: 892

Answers (2)

adaszko
adaszko

Reputation: 188

Try this quick & dirty (GNU) AWK program (save it as zone-file.awk):

/^zone/, /^}/ {
    if (NF == 4) {
        zone = $2
        next
    }

    if (NF == 2 && $1 == "file") {
        sub(";$", "", $2)
        print zone, $2
    }
}

It works for me as follows:

$ awk -f zone-file.awk /etc/named.conf
"." "named.ca"
"localhost" "localhost.zone"
[...]

Upvotes: 2

potong
potong

Reputation: 58391

This might work for you:

sed '/^\s*\(zone\|file\) "\([^"]*\)".*/,//!d;//!d;s//\2/' named.conf
example.com
db.example.com

Upvotes: 3

Related Questions