Sed脚本匹配条件在文件末尾失败

时间:2015-04-29 14:29:37

标签: sed

我使用以下脚本使ip addr每个界面打印一行:

ip addr | sed -nr '/^\w+: /{
    H
    :top
    n
    /^\s/ { H ; b top }
    x
    s/\n//g
    a \ ==========
    p
    b top
  }
'

输入:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth2: <BROADCAST,MULTICAST,NOARP,PROMISC,UP,LOWER_UP> mtu 1600 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:33:58:cb brd ff:ff:ff:ff:ff:ff
    inet6 fe80::a00:27ff:fe33:58cb/64 scope link
       valid_lft forever preferred_lft forever
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:33:58:ca brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.10/24 brd 192.168.122.255 scope global eth0
    inet6 fe80::a00:27ff:fe33:58ca/64 scope link

输出:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00    inet 127.0.0.1/8 scope host lo    inet6 ::1/128 scope host        valid_lft forever preferred_lft forever
 ==========
2: eth2: <BROADCAST,MULTICAST,NOARP,PROMISC,UP,LOWER_UP> mtu 1600 qdisc pfifo_fast state UP qlen 1000    link/ether 08:00:27:33:58:cb brd ff:ff:ff:ff:ff:ff    inet6 fe80::a00:27ff:fe33:58cb/64 scope link        valid_lft forever preferred_lft forever
 ==========

错误:最后一个界面总是丢失。如何解决?

0 个答案:

没有答案