SED 搜索一段文本并在该文本块末尾之前添加文本

SED search for a block of text and add text right before the end of that block of text

需要一些帮助。一直在尝试使用 sed 从 httpd.conf 中搜索一段文本,并在搜索后立即使用 sed 在文本块中添加文本。这是一个例子。

sed -n '/<Directory "\/var\/www\/html">/,/<\/Directory>/p' httpd.conf

之后再次将此输出通过管道传输到 sed 以在其之前插入文本,如下所示:

<Directory "/var/www/html">
. . .
# Limit HTTP methods
<LimitExcept GET POST OPTIONS>
Require all denied
</LimitExcept>
</Directory>

然而在尝试了很多不同的方法之后仍然无法得到我想要的。任何人都可以帮忙吗?希望这不是重复的问题。 谢谢

这可能对您有用 (GNU sed);

sed -i -e '\#<Directory "/var/www/html"#,/<\/Directory>/{/<\/Directory>/i\INSERTED' -e '}' file

使用范围 /first regexp/,/second regexp/command 并使用以下插入命令重复第二个正则表达式。