一致後の文字列の検索

一致後の文字列の検索

与えられた一致の後に文字列を取得する方法はありますか?

たとえば、を実行すると、dmidecode多くの情報が提供されます。例えば

BIOS Information
    Vendor: ABCD
    Version: 123456(V1.01)
    Release Date: 01/01/1970
    Address: 0xE0000
    Runtime Size: 128 kB
    ROM Size: 8192 kB
    Characteristics:
        PCI is supported
        BIOS is upgradeable
        BIOS shadowing is allowed
        Boot from CD is supported
        Selectable boot is supported
        BIOS ROM is socketed
        EDD is supported
        Japanese floppy for NEC 9800 1.2 MB is supported (int 13h)
        Japanese floppy for Toshiba 1.2 MB is supported (int 13h)
        5.25"/360 kB floppy services are supported (int 13h)
        5.25"/1.2 MB floppy services are supported (int 13h)
        3.5"/720 kB floppy services are supported (int 13h)
        3.5"/2.88 MB floppy services are supported (int 13h)
        8042 keyboard services are supported (int 9h)
        CGA/mono video services are supported (int 10h)
        ACPI is supported
        USB legacy is supported
        Targeted content distribution is supported
        UEFI is supported
    BIOS Revision: 1.21
    Firmware Revision: 1.21

「version」をgrepすると、dmidecode出力からさまざまな一致を取得できます。代わりに、^BIOS単語の後に「version」行を検索して最初の一致で停止する方法はありますか?したがって、出力は次のようになります。

Version: 123456(V1.01)

ベストアンサー1

$ sudo dmidecode |
    awk '/^BIOS/ { ++Bios } Bios && /Version/ { print; exit; }'
    Version: 02PI.M505.20110824.LEO
$ 

BIOSが実行したアクションを計算し、バージョンラインでトリガーします。

BIOSが私のシステムの最初のブロックで、grepに最大数のオプションがあるため、この方法も機能します。

sudo dmidecode | grep -m 1 'Version'

おすすめ記事