This is a pretty newbie question but what I am trying to do should be easy :( I am developing a script to automatically install a development environment but I need to comment out loadmodule in the Apache configuration file on my Linux machine.

LoadModule mpm_event_module modules/mod_mpm_event.so

I have been able to remove a comment with the following command:

sudo sed -i '66s/.//' /etc/httpd/conf/httpd.conf

2 Answers 2


Insert comment :

sed 's/^ *LoadModule/#&/' /etc/httpd/conf/httpd.conf

Remove commment :

sed '/^ *# *LoadModule/s/#//' /etc/httpd/conf/httpd.conf

Or to remove spaces too :

sed '/^ *# *LoadModule/s/^ *# *//' /etc/httpd/conf/httpd.conf

To apply substitution (insert or remove) just on a specific line number (not a good idea if you're trying to develop a script to perform automatic installation) :

# Apply on line 66 only
sed '66ba;p;d;:a /^ *# *LoadModule/s/^ *# *//' /etc/httpd/conf/httpd.conf

Or (The same but with -e to make it portable to other sed too) :

sed -e '66ba' -e 'p;d;:a' -e ':a' -e '/^ *# *LoadModule/s/^ *# *//' /etc/httpd/conf/httpd.conf


sed '66{/^ *# *LoadModule/s/^ *# *//}' /etc/httpd/conf/httpd.conf

To edit just on a fixed line number without applying a match:

# Insert comment on line 66
sed '66s/^/#/' /etc/httpd/conf/httpd.conf

# Remove comment on line 66
sed '66s/^#//' /etc/httpd/conf/httpd.conf

# Remove spaces too
sed '66s/^ *# *//' /etc/httpd/conf/httpd.conf

Those will handle config file as :


    LoadModule foo
LoadModule bar


    #LoadModule foo
#LoadModule bar

    #    LoadModule baz
#    LoadModule bax

You can do it with Vim as well:

vim -c '/LoadModule/s/^/#/|x' httpd.conf

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .