SED正则表达式错误 - SED regular expression error

- 此内容更新于:2016-01-25
主题:

我和要修改的文件。这是文本搜索任务:我的任务是改变。正则表达式开始解释:存储任何直到AllowOverride没有:寻找:AllowOverride没有存储任何之后:\(.*\)当我运行以上sed命令的时候,我没有看到任何替代品。我犯什么错误。谢谢

原文:

I am new to sed and want to make modifications in httpd.conf file.

Here is the text to search

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

Task : My task is to change AllowOverride None to 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

Explanation of regular expression

Start with: < Directory "/var/www/html">

store anything till AllowOverride None : \(.*\)

Look for : AllowOverride None

store anything after that : \(.*\)

When I am running above sed command , I am not seeing any replacement. What mistake I am making.

Thanks

解决方案:
您可以使用正则表达式范围选择线的一个子集,然后应用sed命令他们什么?这指定线范围内。线从开始比赛的结束比赛的替换所有匹配的范围内。
原文:

You can use regex ranges to select a subset of lines and then apply sed commands to them

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

What it does?

  • /<Directory "\/var\/www\/html">/, /<\/Directory>/ This specifies the line range. That is lines from start match of /<Directory "\/var\/www\/html">/ to end match of /<\/Directory>/

  • s/AllowOverride None/AllowOverride All/ Does the substitution for all matches of AllowOverride None within the range.

楼主:好了。我要试试这个。你能告诉我什么是我的正则表达式的问题我不会重复同样的错误。

(原文:Nice . I am going to try this . Can you please tell me what is the problem with my regular expression it will me not to repeat the same mistake.)

网友:@voila是sed读取行线和模式匹配。所以,当它到达行说,它不知道是否会发生在线以下。并将只选择直到行结束的,而不是下一行

(原文:@voila The thing is sed reads line be line and does the pattern matching. So when it reaches line say <Directory /var/www/html>, it doesn't know whether AllowOverride is gonna occur in lines following. And .* will select only till the end of the line and not the next line)

楼主:明白了..谢谢你的帮助

(原文:Got it .. Thanks for help)