代码之家  ›  专栏  ›  技术社区  ›  iamauser

使用regex分析lspci树

  •  2
  • iamauser  · 技术社区  · 6 年前
    $ 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订购(插槽)。它们表示为最后一个regex \[[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 回复  |  直到 6 年前
        1
  •  2
  •   Ed Morton    6 年前

    第3个arg的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