根据条件搜索字符串并打印到下一个匹配的字符串

时间:2015-04-30 06:18:15

标签: bash

我正在寻找解决此问题的方法。我得到了一个带有输出的文件,如下所示:

Memory Device
    Array Handle: 0x00E2
    Error Information Handle: No Error
    Total Width: 32 bits
    Data Width: 32 bits
    Size: 512 MB
    Form Factor: DIMM
    Set: None
    Locator: RAM slot #0
    Bank Locator: RAM slot #0
    Type: DRAM
    Type Detail: EDO
    Speed: Unknown
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
Memory Device
    Array Handle: 0x00E2
    Error Information Handle: No Error
    Total Width: Unknown
    Data Width: Unknown
    Size: No Module Installed
    Form Factor: DIMM
    Set: None
    Locator: RAM slot #1
    Bank Locator: RAM slot #1
    Type: DRAM
    Type Detail: Unknown
    Speed: Unknown
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
Memory Device
    Array Handle: 0x00E2
    Error Information Handle: No Error
    Total Width: Unknown
    Data Width: Unknown
    Size: No Module Installed
    Form Factor: DIMM
    Set: None
    Locator: RAM slot #2
    Bank Locator: RAM slot #2
    Type: DRAM
    Type Detail: Unknown
    Speed: Unknown
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
Memory Device
    Array Handle: 0x00E2
    Error Information Handle: No Error
    Total Width: Unknown
    Data Width: Unknown
    Size: No Module Installed
    Form Factor: DIMM
    Set: None
    Locator: RAM slot #3
    Bank Locator: RAM slot #3
    Type: DRAM
    Type Detail: Unknown
    Speed: Unknown
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified

这里,每个段落都以“Memory Device”开头,所以我想搜索每个带有数值的“Size”列的段落,并只打印那些段落。所以,我不需要打印任何尺寸值为“尺寸:没有安装模块”的段落。这是命令"dmidecode --type memory"的部分输出。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

如果输出的格式始终与您可以使用的方式相同

grep -E '[[:blank:]]Size: [0-9]+' mem.txt -A 11 -B 6

[编辑] -B 10只是因为只有第一个条目有数字大小。 -B 6应该更好。感谢您的评论。