使用正则表达式解析lspci树

时间:2018-09-25 14:32:01

标签: regex bash awk sed grep

$ lspci -tv | grep -E 'Gigabit Network|Gigabit Ether| Ethernet' | grep -oP '(?<=^).*(?=Intel)'
           +-01.0-[01-04]----00.0-[02-04]--+-01.0-[03]--+-00.0  
           |                               |            +-00.1  
           |                               |            +-00.2  
           |                               |            \-00.3  
           |                               \-03.0-[04]--+-00.0  
           |                                            \-00.1  
           +-1c.0-[05]----00.0  
           +-1c.1-[06]----00.0  

我正在尝试使用此树中的PCI总线地址获取NIC顺序(插槽)。它们表示为给定行上的最后一个正则表达式\[[0-9a-ZA-Z]\],例如[03],[04],[05],然后是紧跟其后的子标题,例如00.0, 00.1, 00.2代表[03]

我的预期输出应该是:

03:00.0
03:00.1
03:00.2
03:00.3  
04:00.0
04:00.1   
05:00.0
06:00.0

例如,我尝试了此方法,但没有做进一步的尝试。我知道这很丑陋,任何有或没有pipes的解决方案都可以。

$ lspci -tv | grep -E 'Gigabit Network|Gigabit Ether| Ethernet' | grep -oP '(?<=\-).*(?=Intel)'  | grep -oE '(\[[0-9a-ZA-Z]{2}\])|(\[[0-9a-ZA-Z]{2}\].*[0-9]{2}\.[0-9])|(^[0-0]{2}.[0-9])'
[03]--+-00.0
00.1
00.2
00.3
[04]--+-00.0
00.1
[05]----00.0
[06]----00.0

lspci -tv的输出。我只对网卡感兴趣。 grep Network,但还有其他情况,例如Ethernet

$ lspci -tv
-[0000:00]-+-00.0  Intel Corporation Skylake Host Bridge/DRAM Registers
           +-01.0-[01-04]----00.0-[02-04]--+-01.0-[03]--+-00.0  Intel Corporation I350 Gigabit Network Connection
           |                               |            +-00.1  Intel Corporation I350 Gigabit Network Connection
           |                               |            +-00.2  Intel Corporation I350 Gigabit Network Connection
           |                               |            \-00.3  Intel Corporation I350 Gigabit Network Connection
           |                               \-03.0-[04]--+-00.0  Intel Corporation I350 Gigabit Network Connection
           |                                            \-00.1  Intel Corporation I350 Gigabit Network Connection
           +-02.0  Intel Corporation HD Graphics P530
           +-13.0  Intel Corporation Sunrise Point-H Integrated Sensor Hub
           +-14.0  Intel Corporation Sunrise Point-H USB 3.0 xHCI Controller
           +-14.2  Intel Corporation Sunrise Point-H Thermal subsystem
           +-16.0  Intel Corporation Sunrise Point-H CSME HECI #1
           +-16.1  Intel Corporation Sunrise Point-H CSME HECI #2
           +-17.0  Intel Corporation Sunrise Point-H SATA controller [AHCI mode]
           +-1c.0-[05]----00.0  Intel Corporation I210 Gigabit Network Connection
           +-1c.1-[06]----00.0  Intel Corporation I210 Gigabit Network Connection
           +-1c.6-[07-08]----00.0-[08]----00.0  ASPEED Technology, Inc. ASPEED Graphics Family
           +-1f.0  Intel Corporation Sunrise Point-H LPC Controller
           +-1f.2  Intel Corporation Sunrise Point-H PMC
           \-1f.4  Intel Corporation Sunrise Point-H SMBus

1 个答案:

答案 0 :(得分:2)

使用GNU awk作为match()的第三个参数:

$ cat tst.awk
!/Network|Ethernet/ { next }
match($0,/.*\[([0-9]+)\]/,a) {
    nic = a[1]
}
match($0,/.*[+-\\]-([0-9]+\.[0-9]+)  /,a) {
    print nic ":" a[1]
}
$
$ awk -f tst.awk file
03:00.0
03:00.1
03:00.2
03:00.3
04:00.0
04:00.1
05:00.0
06:00.0