0

I am trying to modify an httpd.conf file, allowing override in the /var/www/html directory. For simplicity's sake, let's say it looks like this:

<Directory "/var/www/html">
    # Comment
    AllowOverride none
    # Comment
</Directory>

Trying to replace AllowOverride none with AllowOverride All.

I tried something like this:

sed -i 's/\(Directory \"\/var\/www\/html\">.*?\)AllowOverride none\(.*?<\/Directory>\)/\1AllowOverride all\2/' httpd.conf

It doesn't look like it catches multiple lines, so I tried some other things, such as RegEx modifiers and this: https://www.gnu.org/software/sed/manual/html_node/Text-search-across-multiple-lines.html but was unable to match the desired string.

How would one go about doing this? Solutions outside of sed are welcome as well.

1

I'm going to assume that what you want is to replace ONLY the AllowOverride none specifically in the <Directory "/var/www/html"> block and not elsewhere. We can do that.

Short version:

sed -i '/^<Directory "\/var\/www\/html">/,/^<\/Directory>/ {
    s/AllowOverride none/AllowOverride all/;
}' httpd.conf

This says to only apply the substitute command between the specified lines.

Remember to back up your file first, and diff them after to confirm you got what you wanted. :)

1

sed, being a *nix text tool, is line based. To work upon several lines, you need to take certain measures. Why doesn't

sed 's/AllowOverride none/AllowOverride all/' file

satisfy your needs?

EDIT: or, if you only want to change the string in that "directory" paragraph,

sed '/<Directory "\/var\/www\/html">/,/<\/Directory>/ s/AllowOverride none/AllowOverride all/' file

EDIT: With three small adaptions, you make your own script work correctly:

sed    's/\(Directory \"\/var\/www\/html\">.*?\)AllowOverride none\(.*?<\/Directory>\)/\1AllowOverride all\2/' file
sed -z 's/\(Directory \"\/var\/www\/html\">.*\)AllowOverride none\(.*<\/Directory>\)/\1AllowOverride all\2/' file
     ^                                       ^                        ^ 

-z (--null-data, separate lines by NUL characters; cf. man sed) makes sed ignore line feed chars, i.e. loads the entire file. The two ? indicated by the carets must go away. And, be aware that the " don't need the escape \ (but it doesn't hurt).

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.