Enter("ip as" 명령)
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'