tags:

views:

204

answers:

3

I want to open the httpd.conf file and change the LogFormat line with the new parameters. The criterion will be that the line should start with "LogFormat" and end with the word "combined"

Here is how I do manually. I want to change the line programatically.

vi /etc/httpd/conf/httpd.conf 
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "\"%h\" \"%l\" \"%u\" \"%{%Y-%m-%d %H:%M:%S}t\" \"%r\" \"%>s\" \"%b\" \"%{Referer}i\" \"%{User-Agent}i\" \"%D\" \"%T\" \"%q\" \"%f\" \"%v\" " combined
A: 

You could try something like:

sed 's/^LogFormat.*combined$/new-logformat-line-whatever/' httpd.conf
Jack
Add the -i option to edit the file in-place (rather than writing to stdout).
Geerad
A: 

Use Perl instead, with its -i (inplace-edit) flag.

perl -i.bak -pe 's/^LogFormat (.*) combined$/replacement/' httpd.conf

This will modify the file httpd.conf in place, storing a backup in the file "httpd.conf.bak". Replace "replacement" with the actual replacement text you want.

Sean
Perl,awk,sed , all can do the job.
ghostdog74
+1  A: 
#!/bin/bash

cp /etc/httpd/conf/httpd.conf  /etc/httpd/conf/httpd.conf.bak
awk 'BEGIN{
 pat1="\\\"%{%Y-%m-%d %H:%M:%S}t\\\""
 pat2="\\\"%D\\\" \\\"%T\\\" \\\"%q\\\" \\\"%f\\\" \\\"%v\\\""
}
/^LogFormat.*combined/{
 $5=pat1
 $NF=pat2"\042 combined"
}1' file >temp
mv temp /etc/httpd/conf/httpd.conf
ghostdog74