2013-02-21 37 views
0

過濾命令只返回段有沒有辦法只返回第二或使用grep第三,第四段,awk ...?通過輸出中

我怎樣才能獲得lo段落,我知道這是可能的使用命令ifconfig lo但如何使用,如果其他命令。從ifconfig輸出是:

eth0  Link encap:Ethernet HWaddr 00:25:22:b2:8b:0d 
      inet addr:192.168.1.22 Bcast:192.168.1.255 Mask:255.255.255.0 
      inet6 addr: fe80::225:22ff:feb2:8b0d/64 Scope:Link 
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
      RX packets:100979 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:70753 errors:0 dropped:0 overruns:0 carrier:1 
      collisions:0 txqueuelen:1000 
      RX bytes:129240044 (129.2 MB) TX bytes:7355272 (7.3 MB) 
      Interrupt:29 

lo  Link encap:Local Loopback 
      inet addr:127.0.0.1 Mask:255.0.0.0 
      inet6 addr: ::1/128 Scope:Host 
      UP LOOPBACK RUNNING MTU:16436 Metric:1 
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:0 
      RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) 

回答

3

您可以輕鬆地通過使用awk要麼號段或接口名稱進行篩選。

,第一款數字只是指定紀錄awk 'NR==1' RS=''

# First record 
$ ifconfig | awk 'NR==1' RS='' 
eth0  Link encap:Ethernet HWaddr 00:25:22:b2:8b:0d 
      inet addr:192.168.1.22 Bcast:192.168.1.255 Mask:255.255.255.0 
      inet6 addr: fe80::225:22ff:feb2:8b0d/64 Scope:Link 
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
      RX packets:100979 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:70753 errors:0 dropped:0 overruns:0 carrier:1 
      collisions:0 txqueuelen:1000 
      RX bytes:129240044 (129.2 MB) TX bytes:7355272 (7.3 MB) 
      Interrupt:29 

# Second Record    
$ ifconfig | awk 'NR==2' RS='' 
lo  Link encap:Local Loopback 
      inet addr:127.0.0.1 Mask:255.0.0.0 
      inet6 addr: ::1/128 Scope:Host 
      UP LOOPBACK RUNNING MTU:16436 Metric:1 
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:0 
      RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) 

如果你想通過接口名稱來過濾,而不是記錄編號,然後​​3210:

# Interface lo 
$ ifconfig | awk '$1=="lo"' RS='' 
lo  Link encap:Local Loopback 
      inet addr:127.0.0.1 Mask:255.0.0.0 
      inet6 addr: ::1/128 Scope:Host 
      UP LOOPBACK RUNNING MTU:16436 Metric:1 
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:0 
      RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) 

# Interface eth0               
$ ifconfig | awk '$1=="eth0"' RS='' 
eth0  Link encap:Ethernet HWaddr 00:25:22:b2:8b:0d 
      inet addr:192.168.1.22 Bcast:192.168.1.255 Mask:255.255.255.0 
      inet6 addr: fe80::225:22ff:feb2:8b0d/64 Scope:Link 
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
      RX packets:100979 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:70753 errors:0 dropped:0 overruns:0 carrier:1 
      collisions:0 txqueuelen:1000 
      RX bytes:129240044 (129.2 MB) TX bytes:7355272 (7.3 MB) 
      Interrupt:29 

但我不知道是什麼ifconfig eth0ifconfig lo錯誤。

0

使用awk:

BEGIN { 
    RS = "" 
} 

NR == 3 { 
    print 
    exit 
} 

,打印第三段。將「3」更改爲任何你喜歡的。請注意,將記錄分隔符設置爲空字符串可以使awk在段落上工作,而不是像往常一樣工作。

您可以像這樣運行:

awk -f above.awk input.txt 
+1

你可以改變'NR == 3'到'/^LO [\ t] + /'(我不能在AWK使用'\ s'?)打印有關段落以' 「LO」'。 – 244an 2013-02-21 12:53:19

+0

\ s是簡寫的POSIX字符類的一些工具實現[[:空間:]] - 始終使用[[:空間:]],你不會得到驚喜。 – 2013-02-21 13:36:38

+0

順便說一句,你不需要「+」自1或更多的包括1,所以如果你有'lo'後面有一個空格,它可以重新匹配。無論如何,在這種情況下'$ 1 ==「lo」'可能更清晰。 – 2013-02-21 15:44:53

0

使用pattern range,這裏是一個使用sed的一種方式。它也將刪除最後一個空行:

ifconfig | sed -n '/^lo/,/^$/ { /^$/!p }' 

awk解決方案:

ifconfig | awk '$1=="lo"' RS=