【问题标题】:replace a text inside a block of code in apache2.conf file using sed使用 sed 替换 apache2.conf 文件中的代码块内的文本
【发布时间】:2019-02-07 01:12:44
【问题描述】:

我一直在使用 linux 中一些有用的 sed 命令技巧来替换 apache2.conf 文件中代码块中的一个文本单词。尝试在 google 和 stackoverflow 上搜索已经回答的解决方案。但是,这些人无法真正得到帮助。

这是 apache2.conf 文件中的一段代码。

<FilesMatch "^\.ht">
        Require all denied
</FilesMatch>

我希望将块内的单词 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

我确定我在开始标签内使用的双引号有问题。

任何帮助将不胜感激。 提前致谢。

【问题讨论】:

    标签: linux shell awk sed scripting


    【解决方案1】:

    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>
    

    上面只是在找到起始行时设置一个标志,在找到结束行时清除它,并在设置该标志时将拒绝更改为授予。

    【讨论】:

      【解决方案2】:
      sed -i '/<FilesMatch "^\\.ht">/,\@</FilesMatch>@ s/Require all denied/Require all granted/' /etc/apache2/apache2.conf
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-20
        • 2012-10-14
        • 2018-10-09
        • 1970-01-01
        • 1970-01-01
        • 2017-11-05
        • 2022-10-21
        • 2022-11-14
        相关资源
        最近更新 更多