Operating System - Linux
1753483 Members
4177 Online
108794 Solutions
New Discussion юеВ

Re: sed to edit httpd.conf

 
SOLVED
Go to solution
David Burgess
Esteemed Contributor

sed to edit httpd.conf

I want to edit httpd.conf. Specifically I want to comment out a section of the config file.

For example when I find

Alias /manual/ "/usr/apache/htdocs/manual/"

Options Indexes FollowSymLinks MultiViews
AllowOverride None


I want to end up with

#Alias /manual/ "/usr/apache/htdocs/manual/"
#
# Options Indexes FollowSymLinks MultiViews
# AllowOverride None
#


I'm using sed, but having difficutly in finding the line starting Alias /manual/ and commenting out the next 4 lines too.

Comment out 1 line is

sed -e "/Alias \/manual\//s/^/# DONT'T ALLOW ACCESS TO THE MANUAL #/g" < httpd.conf.orig > httpd.conf

Any ideas would be appreciated.

Regards,
Dave.
5 REPLIES 5
Rodney Hills
Honored Contributor
Solution

Re: sed to edit httpd.conf

Perl can do that fairly easily

perl -p -e 'BEGIN{$start=-99999};$start=$. if /^Alias/; s/^/#/ if $.-$start <5' httpd.conf.orig >httpd.conf

HTH

-- Rod Hills
There be dragons...
David Burgess
Esteemed Contributor

Re: sed to edit httpd.conf

Works a treat and worth 10 points. Can you briefly explain how it works? I can see it's a loop. Wy start at 99999?

Regards,
James R. Ferguson
Acclaimed Contributor

Re: sed to edit httpd.conf

Hi Dave:

Perhaps something like:

# sed -ne '/Alias/,4 s/^/#/;p'

Regards!

...JRF...
Rodney Hills
Honored Contributor

Re: sed to edit httpd.conf

If your text will be bracketed by "Alias" and "", then here is a simpler perl method-

perl -p -e 's/^/#/ if /^Alias/../<\/Directory/' httpd.conf.orig >httpd.conf

HTH

-- Rod Hills

There be dragons...
Rodney Hills
Honored Contributor

Re: sed to edit httpd.conf

My first method works by setting variable $start to the current line number when "Alias" is found, then the "#" is prefixed on any line where the current line number - $start < the number of lines to modify.

The BEGIN{$start=-99999} is because $start will assume to be zero upon first reference, so that the calculation would prefix "#" on the first few lines of the file.

HTH


-- Rod Hills
There be dragons...