Text Processing Sed – How to Find & Replace Pattern Within Specific Pattern Using Sed

sedtext processing

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
</Directory>
...

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

Best Answer

With GNU sed:

sed -i -e '/<Directory "\/var\/www\/html">/,/<\/Directory>/{s/AllowOverride None/AllowOverride All/}' filename
Related Question