IT seems that 'sed' and I will never be friends...

How can I find and replace a pattern within a range (within another pattern)?

In words: Find: AllowOverride None within the following range and replace it with AllowOverride All

<Directory "/var/www/html">
some text
more text
some comments
AllowOverride None
more text

Please note (if not already done): This is related to httpd.conf which contains a lot more "modules" all ending with </Directory>

As this command is used within a yaml file, it would be aesome to achieve this with a one-liner (I am aware, that one-liners are getting ugly pretty fast...).

Also: I rely on using sed (requirement, dont ask me why - pls :))

1 Answer 1


With GNU sed:

sed -i -e '/<Directory "\/var\/www\/html">/,/<\/Directory>/{s/AllowOverride None/AllowOverride All/}' filename
  • It makes so much sense looking at it now. Thank you!
    – frank
    Apr 5, 2015 at 18:00

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.