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
您可以尝试类似:
sed 's/^LogFormat.*combined$/new-logformat-line-whatever/' httpd.conf
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.
#!/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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.