【问题标题】:Search and replace file content using shell script使用 shell 脚本搜索和替换文件内容
【发布时间】:2016-04-09 14:03:47
【问题描述】:

我想搜索和替换文件中的特定值,这里是文件的内容

filename: /etc/httpd/conf/httpd.conf
#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
    AllowOverride none
    Require all denied
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

我想用DocumentRoot /var/www/html/centos替换DocumentRoot /var/www/html

我尝试了以下

sudo sed -i "s#DocumentRoot /var/www/html#DocumentRoot /var/www/html/centos#g" /etc/httpd/conf/httpd.conf

这行不通,谁能指出正确的方向。

谢谢

【问题讨论】:

    标签: linux bash shell centos


    【解决方案1】:

    您要替换的行是:

    DocumentRoot "/var/www/html"
    

    它包含双引号。 sed 命令不包含双引号。试试:

    sudo sed -i 's#DocumentRoot "/var/www/html"#DocumentRoot "/var/www/html/centos"#g' /etc/httpd/conf/httpd.conf
    

    【讨论】:

      【解决方案2】:

      没关系,我想通了,我缺少双引号,以下命令有效

      sudo sed -i 's#DocumentRoot "/var/www/html"#DocumentRoot "/var/www/html/centos"#g' /etc/httpd/conf/httpd.conf
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-29
        • 1970-01-01
        • 1970-01-01
        • 2010-10-13
        • 1970-01-01
        • 2011-02-17
        • 2020-08-03
        • 1970-01-01
        相关资源
        最近更新 更多