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

3 回答 3

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)

如果要按接口名称而不是记录编号进行过滤,则awk '$1=="eth0"' RS=''

# 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 eth0or有什么问题ifconfig lo

于 2013-02-21T13:01:06.920 回答
0

使用模式 range,这是使用sed. 它还将删除最后一个空行:

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

awk解决方案:

ifconfig | awk '$1=="lo"' RS=
于 2013-02-21T12:56:53.193 回答
0

使用 awk:

BEGIN {
  RS = ""
}

NR == 3 {
  print
  exit
}

打印第三段。将“3”更改为您喜欢的任何内容。请注意,将 Record Separator 设置为空字符串会使 awk 像往常一样处理段落而不是行。

你可以像这样运行它:

awk -f above.awk input.txt
于 2013-02-21T12:48:15.207 回答