I am trying to automate the provisioning of an Ubuntu Vagrant box with a Bash script. All is well, but I am having difficulty with configuring Apache to allow overrides from .htaccess files. In my config file, there is the following block:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all

All I need to do is replace AllowOverride None with AllowOverride all. This seems to be a task for sed, but there are other <Directory> blocks in this config file where I do not want to allow override.

How can I use sed to replace a specific string with a new string, when that specific string occurs between two other specific strings?

In other words, how can I replace AllowOverride None only when it appears between <Directory /var/www/> and </Directory>?

I can use the following command to get the block I am interested in:

sed -n '/<Directory \/var\/www\/>/,/<\/Directory>/p' 000-default

sed supports pattern ranges in this form:

sed '/startpattern/,/endpattern/ <sed-commands>' file

For example:

sed '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride all/' httpd.conf
  • This is what I was looking for! – captainblack Jun 11 '19 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.