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

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



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


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.



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