2013-07-02 56 views
0

如何將以下輸出轉換爲sed? 來自:如何將ifconfig輸出連接到每個接口的線路

lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 
    inet 127.0.0.1 netmask ff000000 
igb0: flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRECATED,IPv4,NOFAILOVER> mtu 1500 index 2 
    inet 10.1.1.1 netmask ffffff00 broadcast 10.1.1.255 
    groupname mnic_data 

到:

lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 inet 127.0.0.1 netmask ff000000 
igb0: flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRECATED,IPv4,NOFAILOVER> mtu 1500 index 2 inet 10.1.1.1 netmask ffffff00 broadcast 10.1.1.255 groupname mnic_data 

我已經試過

sed -n '/^[a-z]/{x;1!s/\n/ /g;1!p;};H' 

,但沒有運氣!預先感謝您的幫助

回答

1
ifconfig | 
awk ' 
    FNR>1 && /^[^[:space:]]/ {print ""} 
    {printf "%s", $0} 
    END {print ""} 
' 
+0

非常簡單。謝謝! – IdaWong

0

這是否有幫助(awk)?

awk '/:/&&f{print ""}{printf "%s", $0;f=1}' 

測試

kent$ echo "lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 
    inet 127.0.0.1 netmask ff000000 
igb0: flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRECATED,IPv4,NOFAILOVER> mtu 1500 index 2 
    inet 10.1.1.1 netmask ffffff00 broadcast 10.1.1.255 
    groupname mnic_data"|awk '/:/&&f{print ""}{printf "%s", $0;f=1}' 
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 inet 127.0.0.1 netmask ff000000 
igb0: flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRECATED,IPv4,NOFAILOVER> mtu 1500 index 2 inet 10.1.1.1 netmask ffffff00 broadcast 10.1.1.255 groupname mnic_data 
+0

如果ifconfig發出ipv6地址或mac地址,它們將會冒號。 –

2

你可以試試這個:

$ sed -e :a -e '$!N;s/\n//;ta' -e 'P;D' inputfile 
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 inet 127.0.0.1 netmask ff000000 
igb0: flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRECATED,IPv4,NOFAILOVER> mtu 1500 index 2 inet 10.1.1.1 netmask ffffff00 broadcast 10.1.1.255 groupname mnic_data 

編輯:爲了擺脫上述輸出額外的空間,你可以使用:

$ sed -e :a -e '$!N;s/\n\s\+/ /;ta' -e 'P;D' inputfile 
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 inet 127.0.0.1 netmask ff000000 
igb0: flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRECATED,IPv4,NOFAILOVER> mtu 1500 index 2 inet 10.1.1.1 netmask ffffff00 broadcast 10.1.1.255 groupname mnic_data 
相關問題