SED正则表达式错误

时间:2015-12-19 05:30:46

标签: regex bash shell sed

我是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

正则表达式的解释

从:< Directory "/var/www/html">

开始

存储任何内容,直到AllowOverride无:\(.*\)

寻找:AllowOverride None

之后存储任何内容:\(.*\)

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

由于

1 个答案:

答案 0 :(得分:3)

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

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

它的作用是什么?

  • /<Directory "\/var\/www\/html">/, /<\/Directory>/这指定了行范围。这是从/<Directory "\/var\/www\/html">/的开始匹配到/<\/Directory>/

  • 的结束匹配的行
  • s/AllowOverride None/AllowOverride All/是否替换了AllowOverride None范围内的所有匹配项。