【问题标题】:SED regular expression errorSED 正则表达式错误
【发布时间】:2018-03-14 20:39:52
【问题描述】:

我是sed 的新手,想在httpd.conf 文件中进行修改。

这是要搜索的文本

  <Directory "/var/www/html">

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
  Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess    files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride None   --- Want to change this to AllowOverride All

#
# Controls who can get stuff from this server.
#
   Order allow,deny
    Allow from all

 </Directory>

任务:我的任务是将AllowOverride None改为AllowOverride All

  sed 'N;s:<Directory "/var/www/html">\(.*\)AllowOverride None\(.*\):  <Directory "/var/www/html">\1AllowOverride All\2:' /etc/httpd/conf/httpd.conf  > newHttpdConf.ini

正则表达式解释

开头:&lt; Directory "/var/www/html"&gt;

在 AllowOverride None 之前存储任何内容:\(.*\)

寻找:AllowOverride None

之后存储任何东西:\(.*\)

当我在 sed command 上运行时,我没有看到任何替换。 我犯了什么错误。

谢谢

【问题讨论】:

    标签: regex bash shell sed


    【解决方案1】:

    您可以使用正则表达式范围来选择行的子集,然后对其应用 sed 命令

    $ sed '/<Directory "\/var\/www\/html">/, /<\/Directory>/ s/AllowOverride None/AllowOverride All/' inputFile
    

    它有什么作用?

    • /&lt;Directory "\/var\/www\/html"&gt;/, /&lt;\/Directory&gt;/ 指定行范围。这是从/&lt;Directory "\/var\/www\/html"&gt;/ 的开始匹配到/&lt;\/Directory&gt;/ 的结束匹配的行

    • s/AllowOverride None/AllowOverride All/ 替换范围内AllowOverride None 的所有匹配项。

    【讨论】:

    • 不错。我要试试这个。你能告诉我我的正则表达式有什么问题吗,我不会重复同样的错误。
    • @voila 事情是 sed 读取 line be line 并进行模式匹配。所以当它到达&lt;Directory /var/www/html&gt; 行时,它不知道AllowOverride 是否会出现在后面的行中。而.* 只会选择到行尾而不是下一行
    • 知道了.. 感谢您的帮助
    猜你喜欢
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2010-09-19
    相关资源
    最近更新 更多