2つの文字列間のBash検索文字列

2つの文字列間のBash検索文字列

文字列(インデックス)間のコンテンツを抽出するのに役立ちます。

特に、どのインデックス番号に「HDMI 1」という文字列があるかどうかを確認したいと思います。

Indexcountとインデックス付き段落の間の行数は動的であるため、bashユーティリティを使用して合理的なアプローチを見つけることは困難です。

どのようにこれを達成できるかを提案してください。それではいいと思います!

エル

2 sink(s) available.
    index: 1
        name: <alsa_output.pci-0000_00_1f.3.analog-stereo>
        driver: <module-alsa-card.c>
        flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
        state: SUSPENDED
        suspend cause: IDLE
        priority: 9039
        volume: front-left: 30129 /  46% / -20,25 dB,   front-right: 30129 /  46% / -20,25 dB
                balance 0,00
        base volume: 65536 / 100% / 0,00 dB
        volume steps: 65537
        muted: no
        current latency: 0,00 ms
        max request: 0 KiB
        max rewind: 0 KiB
        monitor source: 1
        sample spec: s16le 2ch 48000Hz
        channel map: front-left,front-right
                     Stereo
        used by: 0
        linked by: 0
        configured latency: 0,00 ms; range is 0,50 .. 1837,50 ms
        card: 0 <alsa_card.pci-0000_00_1f.3>
        module: 24
        properties:
                alsa.resolution_bits = "16"
                device.api = "alsa"
                device.class = "sound"
                alsa.class = "generic"
                alsa.subclass = "generic-mix"
                alsa.name = "ALC623 Analog"
                alsa.id = "ALC623 Analog"
                alsa.subdevice = "0"
                alsa.subdevice_name = "subdevice #0"
                alsa.device = "0"
                alsa.card = "0"
                alsa.card_name = "HDA Intel PCH"
                alsa.long_card_name = "HDA Intel PCH at 0x6001110000 irq 136"
                alsa.driver_name = "snd_hda_intel"
                device.bus_path = "pci-0000:00:1f.3"
                sysfs.path = "/devices/pci0000:00/0000:00:1f.3/sound/card0"
                device.bus = "pci"
                device.vendor.id = "8086"
                device.vendor.name = "Intel Corporation"
                device.product.id = "f0c8"
                device.form_factor = "internal"
                device.string = "front:0"
                device.buffering.buffer_size = "352800"
                device.buffering.fragment_size = "176400"
                device.access_mode = "mmap+timer"
                device.profile.name = "analog-stereo"
                device.profile.description = "Analog Stereo"
                device.description = "Built-in Audio Analog Stereo"
                module-udev-detect.discovered = "1"
                device.icon_name = "audio-card-pci"
        ports:
                analog-output-speaker: Speakers (priority 10000, latency offset 0 usec, available: unknown)
                        properties:
                                device.icon_name = "audio-speakers"
                analog-output-headphones: Headphones (priority 9900, latency offset 0 usec, available: no)
                        properties:
                                device.icon_name = "audio-headphones"
        active port: <analog-output-speaker>
  * index: 2
        name: <alsa_output.hw_0_7>
        driver: <module-alsa-sink.c>
        flags: HARDWARE DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
        state: RUNNING
        suspend cause: (none)
        priority: 9030
        volume: front-left: 65536 / 100% / 0,00 dB,   front-right: 65536 / 100% / 0,00 dB
                balance 0,00
        base volume: 65536 / 100% / 0,00 dB
        volume steps: 65537
        muted: no
        current latency: 40,43 ms
        max request: 7 KiB
        max rewind: 7 KiB
        monitor source: 3
        sample spec: s16le 2ch 48000Hz
        channel map: front-left,front-right
                     Stereo
        used by: 1
        linked by: 1
        configured latency: 40,00 ms; range is 0,50 .. 1837,50 ms
        module: 25
        properties:
                alsa.resolution_bits = "16"
                device.api = "alsa"
                device.class = "sound"
                alsa.class = "generic"
                alsa.subclass = "generic-mix"
                alsa.name = "HDMI 1"
                alsa.id = "HDMI 1"
                alsa.subdevice = "0"
                alsa.subdevice_name = "subdevice #0"
                alsa.device = "7"
                alsa.card = "0"
                alsa.card_name = "HDA Intel PCH"
                alsa.long_card_name = "HDA Intel PCH at 0x6001110000 irq 136"
                alsa.driver_name = "snd_hda_intel"
                device.bus_path = "pci-0000:00:1f.3"
                sysfs.path = "/devices/pci0000:00/0000:00:1f.3/sound/card0"
                device.bus = "pci"
                device.vendor.id = "8086"
                device.vendor.name = "Intel Corporation"
                device.product.id = "f0c8"
                device.form_factor = "internal"
                device.string = "hw:0,7"
                device.buffering.buffer_size = "352800"
                device.buffering.fragment_size = "176400"
                device.access_mode = "mmap+timer"
                device.description = "Built-in Audio"
                device.icon_name = "audio-card-pci"

答え1

grepを使用してインデックス番号と検索名のみを抽出します。名前の上のインデックス番号は目的のインデックス番号なので、grepの-B(以前の)コンテキストを使用して抽出し、head名前の行を削除します.

grep 'index:\|name = "HDMI 1"' file | grep -B1 'HDMI 1' | head -n1

答え2

インデックスを変数に保存し、行が一致した場合はその変数を印刷しますHDMI。ここでawkは良い選択です。

$ awk '{ if (/index:/) {ind=$NF} if(/HDMI/){print ind,$0}}' file
2                 alsa.name = "HDMI 1"
2                 alsa.id = "HDMI 1"

またはパール:

$ perl -ne '$ind=$1 if /index:\s*(\d+)/; if(/HDMI/){print "$ind $_"}' file
2                 alsa.name = "HDMI 1"
2                 alsa.id = "HDMI 1"

答え3

使用sed

$ sed -En '/index/{h;};/HDMI 1/{x;p;q;}' input_file
  * index: 2

以前に見たインデックスを保持HDMI 1バッファに保存し、マッチし、行を保持バッファHDMI 1の内容に置き換え、インデックスを印刷してすぐに終了します。

答え4

すべての発生を一覧表示しますHDMI 1

$ sed -En '/index/{h;};/HDMI 1/{G;s/\n//;p;}' infile
                alsa.name = "HDMI 1"  * index: 2
                alsa.id = "HDMI 1"  * index: 2

関連情報