sed在两个括号之间的最后一行插入第二行

sed在两个括号之间的最后一行插入第二行,sed,Sed,我有一个包含以下信息的区域文件: zone "example.com" { type master; file "master/example.com"; }; zone "example.org" { type master; file "master/example.org"; }; 等等。我需要grepexample.com并在括号内添加三行 我可以用这个来获得整个括号: sed -n '/zone \"example.com\" {/,/};/p' zo

我有一个包含以下信息的区域文件:

zone "example.com" {
    type master;
    file "master/example.com";
};
zone "example.org" {
    type master;
    file "master/example.org";
};
等等。我需要grepexample.com并在括号内添加三行

我可以用这个来获得整个括号:

sed -n '/zone \"example.com\" {/,/};/p' zones.local
结果:

zone "example.com" {
        type master;
        file "master/example.com";
};
我需要添加以下三行,以便最终结果如下所示:

zone "example.com" {
        type master;
        file "master/example.com";
        key-directory "/etc/bind/keys/example.com";
        inline-signing yes;
        auto-dnssec maintain;
};
zone "example.org" {
        type master;
        file "master/example.org";
};
但不管我怎么努力,我都无法让它发挥作用。 我试过这个例子:

sed -n '/zone \"example.com\" {/,/};/p; $i key-directory "/etc/bind/keys/example.com";' zones.local
但这将添加到整个文件的第二行,而不是第一个正则表达式的第二行。 我能让它在第一个正则表达式中添加行吗

我完全陷入困境,非常感谢您的帮助

这可能适合您(GNU-sed):

关注
one“example.com”{
}之间的文件行,所有其他行将正常打印

当遇到以
zone“example.com”{
开头的行时,请使用
h
命令进行复制,然后打印当前行并使用
n
命令将其替换为下一行

如果当前行没有开始
,重复此操作直到完成,然后切换到复制的最后一行,使用
x
命令和替换命令
s
将不带空格的文本替换到行末,并使用所需的新文本进行打印。注意替换命令上的
p
标记。这将保留缩进,并有效地在结束前插入文本


对要插入的其余新文本重复上述操作,最后交换回模式空间并打印。

使用
sed
解析结构化数据(如ISC绑定配置文件)可能不是最好的方法。使用专用的解析器来完成这类任务,例如,或者感谢您提供的提示。我一定会留意的!妈的,这是我得到的最好的答案!谢谢你,也谢谢你的完美解释。
sed '/^zone "example.com" {/{:a;h;n;/^};/!ba;x;s#\S.*#key-directory "/etc/bind/keys/example.com";#p;s/\S.*/inline-signing yes;/p;s/\S.*/auto-dnssec maintain;/p;x}' file