Search and comment block of text from apache httpd.conf

I want to search for a block of text in httpd.conf that between two strings and comment it. There are multiple blocks with "<Directory.. and </Directory>"

 
<Directory "${ORACLE_INSTANCE}/config/${COMPONENT_TYPE}/${COMPONENT_NAME}/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# core - Apache HTTP Server Version 2.2
# for more information.
#
Options FollowSymLinks
#
# 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
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
DirectoryIndex welcome-index.html
</Directory>

I am able to search for desired block, but I am unable to add comment to the lines found.

sed -n '/^<Directory.*htdocs">$/,/<\/Directory>/p' httpd.conf

Presuming that the sed-command shows the lines you are interested in just add a small action to it:

sed '/^<Directory.*htdocs">$/,/<\/Directory>/ {
                 s/^/# /
                }' httpd.conf

This replaces the beginning of lines ("^") with comment sign and a space ("# ").

I hope this helps.

bakunin

That works appreciate your help.

How can I add comment to the searched lines only when '#' does not exist already?

Small adaption to bakunin's fine script. Try

sed '/^<Directory.*htdocs">$/,/<\/Directory>/ { s/^[^#]/#&/}' file

or

sed '/^<Directory.*htdocs">$/,/<\/Directory>/ {/^#/!s/^/#/}' file