voila
voila

Reputation: 1666

SED regular expression error

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

Upvotes: 0

Views: 4526

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

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.

Upvotes: 3

Related Questions