输入(“ip a s”命令)
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether e3:1c:12:f3:5a:34 brd ff:ff:ff:ff:ff:ff
inet 1.2.3.4/24 brd 1.2.3.255 scope global eth0
inet6 fe80::e61f:13ff:fef3:5a34/64 scope link
valid_lft forever preferred_lft forever
输出:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether e3:1c:12:f3:5a:34 brd ff:ff:ff:ff:ff:ff inet 1.2.3.4/24 brd 1.2.3.255 scope global eth0 inet6 fe80::e61f:13ff:fef3:5a34/64 scope link valid_lft forever preferred_lft forever
问题:如何删除输入/输出中的新行?
答案1
使用-o
/-oneline
选项。这正是它的用途。
# ip -o addr show
1: lo inet 127.0.0.1/8 scope host lo\ valid_lft forever preferred_lft forever
1: lo inet6 ::1/128 scope host \ valid_lft forever preferred_lft forever
4: wlan0 inet 192.168.0.24/24 brd 192.168.0.255 scope global wlan0\ valid_lft forever preferred_lft forever
4: wlan0 inet6 fe80::c685:8ff:fe80:dd06/64 scope link \ valid_lft forever preferred_lft forever
5: docker0 inet 172.17.42.1/16 scope global docker0\ valid_lft forever preferred_lft forever
5: docker0 inet6 fe80::5484:7aff:fefe:9799/64 scope link \ valid_lft forever preferred_lft forever
23: veth3d95 inet6 fe80::8429:16ff:fed6:beb3/64 scope link \ valid_lft forever preferred_lft forever
答案2
您可以使用perl
:
$ ip a s | perl -00pe 's/\n *(?![0-9])/ /g'
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 valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether f4:6d:04:11:43:fc brd ff:ff:ff:ff:ff:ff
答案3
Perl/sed 专家面前的丑陋解决方案
ip a s| awk 'BEGIN { s=0 ; } /^[^\t ]/ {if (s>0) printf "\n" ; s++ ; } {printf " %s",$0;} END { printf "\n" ;}'
基本上
BEGIN { s=0 ; }
线路计数器
/^[^\t ]/ {if (s>0) printf "\n" ; s++ ; }
如果行以非空白且不是 forst 开头,则发出换行符
{printf " %s",$0;}
复制不带换行符的行
END { printf "\n" ;}
最后,使用一个新行。
答案4
如果你愿意sed
你可以尝试这个解决方案,其中也有一个很好的解释:
您可以使用^[[:digit:]]
因为 的输出ip
。
ip addr show | sed -e '/^[[:digit:]]/bpp' -e 'H;$bpp' -e 'd' -e ':pp' -e 'x;/^$/d;s/\n/ /g'