0

I'm trying to replace a specific "AllowOverride None" to "AllowOverride All" in the httpd.conf file.

Given the file has multiple line with the same pattern I thought I would do a multiline pattern to replace it... Unless sed has a way to choose when it should replace the pattern.

#
# 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

to:

#
# 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 All

I was replacing single lines with:

sudo sed -i 's|DocumentRoot "/var/www/html"|DocumentRoot "/var/www/html/test"|' /etc/httpd/conf/httpd.conf

But I'm not sure how to do it if there's multiple lines

Thanks

  • I'm not sure how your sed line will change AllowOverride None to AllowOverride All, can you elaborate? Also are you looking for the g flag which tells sed to replace all matches and not just the first? 's/pattern/replace/g' – Jesse_b Oct 6 '17 at 16:25
  • Actually as I was writing the question I found out you can choose a specific line to replace: sudo sed -i '151s/AllowOverride None/AllowOverride All/' httpd.conf works perfectly – Radizzt Oct 6 '17 at 16:27
1

sed has many ways to do this. For example, with the change command:

sed --in-place '/^AllowOverride All/c\
AllowOverride None\n' httpd.conf

Or, with a better use of substitute:

sed --in-place '/^AllowOverride/s/All/None/'

The latter can be transliterated as 'On lines which begin with AllowOverride, replace the first instance of All with None'.

0

Turn out you can specify a line number to replace a string:

sudo sed -i '151s/AllowOverride None/AllowOverride All/' httpd.conf

works perfectly.

  • 1
    I don't see the purpose of this. What if it's on a different line the next time you try it? If you have to manually parse the file to find the correct line number you might as well just manually change the config while you are in there. – Jesse_b Oct 6 '17 at 16:37

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.