我想AllowOverride None
从具有路径的组中进行替换/var/www/
。我使用 sed 来执行此操作,但它会替换每个匹配项。
apache2.conf在运行脚本之前
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
apache2.conf运行脚本后
<Directory />
Options FollowSymLinks
AllowOverride All
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride All
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
脚本
#!/bin/bash
sed "s/AllowOverride None/AllowOverride All/g" apache2.conf
我的问题是:我怎样才能告诉 sed 替换它所在的位置/var/www/
?
答案1
您可以将替代模式与范围匹配相结合。
sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/{s/AllowOverride None/AllowOverride All/g}' file.txt