红联Linux门户
Linux帮助

sed 替换问题

发布时间:2015-02-07 00:37:18来源:红联作者:t12345pk
Linux 命令:
sed 's/context/eth0 Link encap:Ethernet HWaddr 00:50:56:8b:08:3f inet addr:192.168.0.32 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::250:56ff:fe8b:83f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:20676675 errors:0 dropped:101 overruns:0 frame:0 TX packets:22420897 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2923831448 (2.9 GB) TX bytes:17329976810 (17.3 GB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:1281994 errors:0 dropped:0 overruns:0 frame:0 TX packets:1281994 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:158591692 (158.5 MB) TX bytes:158591692 (158.5 MB)/g' test.xml

将test.xml中字符串 "context" 替换成:
--------------- 以下数据-------------
eth0 Link encap:Ethernet HWaddr 00:00:00:00:08:3f inet addr:192.168.0.254 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::555:55ff:fe8b:55f/55 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:20676675 errors:0 dropped:101 overruns:0 frame:0 TX packets:22420897 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2923831448 (2.9 GB) TX bytes:17329976810 (17.3 GB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:1281994 errors:0 dropped:0 overruns:0 frame:0 TX packets:1281994 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:158591692 (158.5 MB) TX bytes:158591692 (158.5 MB)
----------------------------------------
报错:
sed: -e expression #1, char 162: unknown option to `s'

请问该如何解决 , 其中替换的文本是通过ifconfig 采集而来
有什么办法能替换成功吗?
文章评论

共有 1 条评论

  1. xiemengguilin 于 2015-02-23 16:11:00发表:

    问题在于字符串中有“/”,sed会将其解释为字符串结束,需要在前面加转义符号“\”。要修改文件要加选项“-i”,可是使用如下指令。
    sed -i "s/context/'eth0 Link encap:Ethernet HWaddr 00:50:56:8b:08:3f inet addr:192.168.0.32 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::250:56ff:fe8b:83f\/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:20676675 errors:0 dropped:101 overruns:0 frame:0 TX packets:22420897 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2923831448 (2.9 GB) TX bytes:17329976810 (17.3 GB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1\/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:1281994 errors:0 dropped:0 overruns:0 frame:0 TX packets:1281994 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:158591692 (158.5 MB) TX bytes:158591692 (158.5 MB)'/g" test.xml