gpt4 book ai didi

regex - 使用 sed 一个接一个地添加一个文本 block

转载 作者:太空狗 更新时间:2023-10-29 11:18:49 24 4
gpt4 key购买 nike

我想添加以下文本 block //这里有更多的文字 区域“mysite.com”{ 类型大师; 文件“mysite.com.fwd”; 允许更新{无; }; };

就在下面显示的文本 block 之后

zone "." IN {
type hint;
file "named.ca";
};

文件最终应该如下所示

 //There are more lines up here
zone "." IN {
type hint;
file "named.ca";
};

zone "mysite.com" {
type master;
file "mysite.com.fwd";
allow-update { none; };
};
//There are more lines below here

这里的困难在于要匹配的模式中的 \n 不适用于 sed。

感谢您在高级方面的帮助

最佳答案

我会这样做:

sed "/^};/a \ \nzone \"mysite.com\" {\n type master;\n file \"mysite.com.fwd\";\n allow-update { none; };\n};\n" test.conf

之前

zone "." IN {
type hint;
file "named.ca";
};

之后

zone "." IN {
type hint;
file "named.ca";
};

zone "mysite.com" {
type master;
file "mysite.com.fwd";
allow-update { none; };
};

^}; 是这里的一个模式,之后您可以使用 a 附加您的新 block ,如果需要,您可以更改模式。

另一种更灵活的 sed 方法 - 使用文件,在其中你有新的 block :

sed "/^};/r block.conf" test.conf

这里我们只是使用读取命令 rblock.conf 中的文本添加到 test.conf 中。 /^};/ 又是你的模式。

这个范围模式应该是 uniqe /file\"named.ca\";/,/^};/ 我想这应该对你有用

sed "/file \"named.ca\";/,/^};/ {
/file*/n
a \ \nzone \"mysite.com\" {\n type master;\n file \"mysite.com.fwd\";\n allow-update { none; };\n};\n
}
" test.conf

//,// 结构表示从具有适当模式的行开始并以另一行结束的范围模式。在我们的例子中,它是 2 行模式,我们想跳过第一行,这里是 /file*/n。然后我们将我们的 block 附加到从空行开始的第二行的和处。

一些有用的方法也在这里:How to sed a block of text with specific pattern?

关于regex - 使用 sed 一个接一个地添加一个文本 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27428955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com