怎样用脚本修改conf中的某一项的值

怎样用脚本修改conf中的某一项的值

linux下有许多conf文件,我们就以httpd.conf为例

试修改其中:
ServerAdmin [email="root@localhost"]root@localhost[/email]

修改为:
ServerAdmin [email="usename@192.168.0.123"]usename@192.168.0.123[/email]

即把[email="root@localhost"]root@localhost[/email]改为[email="username@192.168.0.123"]username@192.168.0.123[/email]

其它的东西都不动(包括其中的以#开始的注掉的行)

HOW?
先行谢过!      
复制内容到剪贴板
代码:
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[25894 0] #[/color] cat f
... ...
ServerAdmin [color=red]root@localhost[/color]
... ...
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[25894 0] #[/color] sed -e '/ServerAdmin root@localhost/ s/root@localhost/username@192.168.0.123/' f
... ...
ServerAdmin [color=red]username@192.168.0.123[/color]
... ...
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[25894 0] #[/color] o
      
复制内容到剪贴板
代码:
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[25894 0] #[/color] cat f
... ...
ServerAdmin root@localhost
... ...
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[25894 0] #[/color] sed -e 's/\(ServerAdmin \)root@localhost/\1username@192.168.0.123/' f
... ...
ServerAdmin username@192.168.0.123
... ...
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[25894 0] #[/color] o
      
又把问题具体化了,sorry,
其中的root@localhost是一个未知、不确定的值,
我的意思是修改ServerAdmin后的一个值,而username@192.168.0.123是一个已知值      
seems like u r a little lazy
复制内容到剪贴板
代码:
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[25894 0] #[/color] cat f
... ...
ServerAdmin root@localhost
ServerAdmin user@host
... ...
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[25894 0] #[/color] sed -e 's/^\(ServerAdmin \).*/\1username@192.168.0.123/' f
... ...
ServerAdmin username@192.168.0.123
ServerAdmin username@192.168.0.123
... ...
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[25894 0] #[/color] o
      
:p 我已经把shell和c搞混淆了      
[QUOTE]我已经把shell和c搞混淆了[/QUOTE]第一次听说能把 C 跟 Shell 搞混了的       
我说的是思维上      
由于要替换的是变量,并且,在ServerAdmin前可能有些空格,TAB,等东西,最后还是用c还解决的,想偷懒都不行!      
复制内容到剪贴板
代码:
[0 No.2048 huan@huan ~]$ cat txt
# line 1
ServerAdmin  what ever
# line 2  ServerAdmin this will not be changed

[0 No.2049 huan@huan ~]$ perl -pi -e 's/(ServerAdmin\s+).*/$1 username\@192.168.0.123/ unless /^#/ ' txt

[0 No.2050 huan@huan ~]$ cat txt
# line 1
ServerAdmin   username@192.168.0.123
# line 2  ServerAdmin this will not be changed

[0 No.2051 huan@huan ~]$