I want to replace one line from my configuration file with the new settings.
The file Name: /etc/httpd/conf/httpd.conf
The following line should be replaced with the line mentioned below.
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "\"%h\" \"%l\" \"%u\" \"%t\" \"%r\" \"%>s\" \"%b\" \"%{Referer}i\" \"%{User-Agent}i\"" combined
I want to accomplish this task using sed and I do not want to use vi
how about you try it in awk...
awk -v f="\"" '{printf("%s %c",$1,f);nf=$NF;for(i=2;i<NF;i++){
gsub(f,"",$i);printf(" \\%c%s\\%c",f,$i,f)}{printf("%c %s\n",f,nf)}}' infile
Just double the \ and it's done ^^
sed 's:LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined:LogFormat "\\"%h\\" \\"%l\\" \\"%u\\" \\"%t\\" \\"%r\\" \\"%>s\\" \\"%b\\" \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined:g' yourfile
thegeek
4
It would look more decent when you do with perl !
perl -e 'while(<>) { s/\QLogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined\E/\QLogFormat "\"%h\" \"%l\" \"%u\" \"%t\" \"%r\" \"%>s\" \"%b\" \"%{Referer}i\" \"%{User-Agent}i\"" combined\E/;print }' httpd.conf