Sed script match condition fails at the end of file -


i'm using following script make ip addr print 1 line per interface:

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

input:

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 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 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 

output:

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 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  ========== 

the bug: last interface missing. how fix it?

good grief, use awk:

$ awk -v rs= '{gsub(/\n\s+/," ")}1' file 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 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 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 

sed simple substitutions on individual lines, not involving multiple lines. of seds arcane constructs things spanning multiple lines became obsolete in mid-1970s when awk invented. people use them today mental exercise of seeing if can solve puzzle sed - not use resulting incantation in code.

change \s [[:space:]] above if awk doesn't support \s (gawk does).


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -