我一直在使用 linux 中有用的 sed 命令的一些技巧来替换 apache2.conf 文件中代码块中的一个文本词。尝试在 google 和 stackoverflow 上搜索已经回答的解决方案。但是,那些人无法真正获得帮助。
这是 apache2.conf 文件中的一段代码。
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
我希望将 block 内的单词 denied 更改为 granted。如下所示,
<FilesMatch "^\.ht">
Require all granted
</FilesMatch>
我正在尝试使用命令
sudo sed -i "/<IfModule "^\.ht">/,\@</IfModule>@ s/Require all denied/Require all granted/" /etc/apache2/apache2.conf
而且我确定我在开始标记内使用的双引号处有误。
任何帮助将不胜感激。 提前致谢。
最佳答案
sed 用于在单独的行上执行 s/old/new,仅此而已。对于其他任何你应该使用 awk 的东西:
$ awk '/<FilesMatch "\^\\\.ht">/{f=1} f{sub(/denied/,"granted")} /<\/FilesMatch>/{f=0} 1' file
<FilesMatch "^\.ht">
Require all granted
</FilesMatch>
上面只是在找到起始行时设置一个标志,在找到结束行时清除它,并在设置该标志时拒绝更改为授予。
关于linux - 使用 sed 替换 apache2.conf 文件中代码块内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52125011/