首页 > 解决方案 > 从终端输出中获取多行

问题描述

我想ports在下面为每个设备提取:

    index: 0
    name: <alsa_output.pci-0000_00_1f.3-platform-skl_hda_dsp_generic.HiFi__hw_sofhdadsp_5__sink>
    driver: <module-alsa-card.c>
    flags: HARDWARE DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
    state: SUSPENDED
    suspend cause: IDLE
    priority: 9037
    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: 0,00 ms
    max request: 0 KiB
    max rewind: 0 KiB
    monitor source: 0
    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 .. 341,00 ms
    card: 0 <alsa_card.pci-0000_00_1f.3-platform-skl_hda_dsp_generic>
    module: 7
    properties:
        alsa.resolution_bits = "16"
        device.api = "alsa"
        device.class = "sound"
        alsa.class = "generic"
        alsa.subclass = "generic-mix"
        alsa.name = ""
        alsa.id = "HDMI3 (*)"
        alsa.subdevice = "0"
        alsa.subdevice_name = "subdevice #0"
        alsa.device = "5"
        alsa.card = "0"
        alsa.card_name = "sof-hda-dsp"
        alsa.long_card_name = "DellInc.-Inspiron5490--0YMVR1"
        alsa.driver_name = "snd_soc_skl_hda_dsp"
        device.bus_path = "pci-0000:00:1f.3-platform-skl_hda_dsp_generic"
        sysfs.path = "/devices/pci0000:00/0000:00:1f.3/skl_hda_dsp_generic/sound/card0"
        device.bus = "pci"
        device.vendor.id = "8086"
        device.vendor.name = "Intel Corporation"
        device.product.id = "02c8"
        device.string = "hw:sofhdadsp,5"
        device.buffering.buffer_size = "65472"
        device.buffering.fragment_size = "16320"
        device.access_mode = "mmap+timer"
        device.profile.name = "HiFi: hw:sofhdadsp,5: sink"
        device.profile.description = "HDMI3/DP3 Output"
        alsa.mixer_device = "hw:sofhdadsp"
        device.description = "sof-hda-dsp HDMI3/DP3 Output"
        module-udev-detect.discovered = "1"
        device.icon_name = "audio-card-pci"
    ports:
        [Out] HDMI3: HDMI3/DP3 Output (priority 700, latency offset 0 usec, available: no)
            properties:
                
    active port: <[Out] HDMI3>
    index: 1
    name: <alsa_output.pci-0000_00_1f.3-platform-skl_hda_dsp_generic.HiFi__hw_sofhdadsp_4__sink>
    driver: <module-alsa-card.c>
    flags: HARDWARE DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
    state: SUSPENDED
    suspend cause: IDLE
    priority: 9036
    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: 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 .. 341,00 ms
    card: 0 <alsa_card.pci-0000_00_1f.3-platform-skl_hda_dsp_generic>
    module: 7
    properties:
        alsa.resolution_bits = "16"
        device.api = "alsa"
        device.class = "sound"
        alsa.class = "generic"
        alsa.subclass = "generic-mix"
        alsa.name = ""
        alsa.id = "HDMI2 (*)"
        alsa.subdevice = "0"
        alsa.subdevice_name = "subdevice #0"
        alsa.device = "4"
        alsa.card = "0"
        alsa.card_name = "sof-hda-dsp"
        alsa.long_card_name = "DellInc.-Inspiron5490--0YMVR1"
        alsa.driver_name = "snd_soc_skl_hda_dsp"
        device.bus_path = "pci-0000:00:1f.3-platform-skl_hda_dsp_generic"
        sysfs.path = "/devices/pci0000:00/0000:00:1f.3/skl_hda_dsp_generic/sound/card0"
        device.bus = "pci"
        device.vendor.id = "8086"
        device.vendor.name = "Intel Corporation"
        device.product.id = "02c8"
        device.string = "hw:sofhdadsp,4"
        device.buffering.buffer_size = "65472"
        device.buffering.fragment_size = "16320"
        device.access_mode = "mmap+timer"
        device.profile.name = "HiFi: hw:sofhdadsp,4: sink"
        device.profile.description = "HDMI2/DP2 Output"
        alsa.mixer_device = "hw:sofhdadsp"
        device.description = "sof-hda-dsp HDMI2/DP2 Output"
        module-udev-detect.discovered = "1"
        device.icon_name = "audio-card-pci"
    ports:
        [Out] HDMI2: HDMI2/DP2 Output (priority 600, latency offset 0 usec, available: no)
            properties:
                
    active port: <[Out] HDMI2>
    index: 2
    name: <alsa_output.pci-0000_00_1f.3-platform-skl_hda_dsp_generic.HiFi__hw_sofhdadsp_3__sink>
    driver: <module-alsa-card.c>
    flags: HARDWARE DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
    state: SUSPENDED
    suspend cause: IDLE
    priority: 9035
    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: 0,00 ms
    max request: 0 KiB
    max rewind: 0 KiB
    monitor source: 2
    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 .. 341,00 ms
    card: 0 <alsa_card.pci-0000_00_1f.3-platform-skl_hda_dsp_generic>
    module: 7
    properties:
        alsa.resolution_bits = "16"
        device.api = "alsa"
        device.class = "sound"
        alsa.class = "generic"
        alsa.subclass = "generic-mix"
        alsa.name = ""
        alsa.id = "HDMI1 (*)"
        alsa.subdevice = "0"
        alsa.subdevice_name = "subdevice #0"
        alsa.device = "3"
        alsa.card = "0"
        alsa.card_name = "sof-hda-dsp"
        alsa.long_card_name = "DellInc.-Inspiron5490--0YMVR1"
        alsa.driver_name = "snd_soc_skl_hda_dsp"
        device.bus_path = "pci-0000:00:1f.3-platform-skl_hda_dsp_generic"
        sysfs.path = "/devices/pci0000:00/0000:00:1f.3/skl_hda_dsp_generic/sound/card0"
        device.bus = "pci"
        device.vendor.id = "8086"
        device.vendor.name = "Intel Corporation"
        device.product.id = "02c8"
        device.string = "hw:sofhdadsp,3"
        device.buffering.buffer_size = "65472"
        device.buffering.fragment_size = "16320"
        device.access_mode = "mmap+timer"
        device.profile.name = "HiFi: hw:sofhdadsp,3: sink"
        device.profile.description = "HDMI1/DP1 Output"
        alsa.mixer_device = "hw:sofhdadsp"
        device.description = "sof-hda-dsp HDMI1/DP1 Output"
        module-udev-detect.discovered = "1"
        device.icon_name = "audio-card-pci"
    ports:
        [Out] HDMI1: HDMI1/DP1 Output (priority 500, latency offset 0 usec, available: yes)
            properties:
                device.product.name = "2460G5"
    active port: <[Out] HDMI1>
  * index: 3
    name: <alsa_output.pci-0000_00_1f.3-platform-skl_hda_dsp_generic.HiFi__hw_sofhdadsp__sink>
    driver: <module-alsa-card.c>
    flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
    state: RUNNING
    suspend cause: (none)
    priority: 9032
    volume: front-left: 56752 /  87% / -3,75 dB,   front-right: 56752 /  87% / -3,75 dB
            balance 0,00
    base volume: 65536 / 100% / 0,00 dB
    volume steps: 65537
    muted: no
    current latency: 19,83 ms
    max request: 4 KiB
    max rewind: 4 KiB
    monitor source: 3
    sample spec: s16le 2ch 48000Hz
    channel map: front-left,front-right
                 Stereo
    used by: 1
    linked by: 1
    configured latency: 23,22 ms; range is 0,50 .. 341,00 ms
    card: 0 <alsa_card.pci-0000_00_1f.3-platform-skl_hda_dsp_generic>
    module: 7
    properties:
        alsa.resolution_bits = "16"
        device.api = "alsa"
        device.class = "sound"
        alsa.class = "generic"
        alsa.subclass = "generic-mix"
        alsa.name = ""
        alsa.id = "HDA Analog (*)"
        alsa.subdevice = "0"
        alsa.subdevice_name = "subdevice #0"
        alsa.device = "0"
        alsa.card = "0"
        alsa.card_name = "sof-hda-dsp"
        alsa.long_card_name = "DellInc.-Inspiron5490--0YMVR1"
        alsa.driver_name = "snd_soc_skl_hda_dsp"
        device.bus_path = "pci-0000:00:1f.3-platform-skl_hda_dsp_generic"
        sysfs.path = "/devices/pci0000:00/0000:00:1f.3/skl_hda_dsp_generic/sound/card0"
        device.bus = "pci"
        device.vendor.id = "8086"
        device.vendor.name = "Intel Corporation"
        device.product.id = "02c8"
        device.string = "hw:sofhdadsp"
        device.buffering.buffer_size = "65472"
        device.buffering.fragment_size = "16320"
        device.access_mode = "mmap+timer"
        device.profile.name = "HiFi: hw:sofhdadsp: sink"
        device.profile.description = "Speaker + Headphones"
        alsa.mixer_device = "hw:sofhdadsp"
        device.description = "sof-hda-dsp Speaker + Headphones"
        module-udev-detect.discovered = "1"
        device.icon_name = "audio-card-pci"
    ports:
        [Out] Speaker: Speaker (priority 100, latency offset 0 usec, available: unknown)
            properties:
                
        [Out] Headphones: Headphones (priority 200, latency offset 0 usec, available: yes)
            properties:
                
    active port: <[Out] Headphones>

我可以通过以下方式获得每个设备index

pacmd list-sinks | awk '/index:/ {print $0}

或与:

pacmd list-sinks | grep -e index

这导致:

    index: 0
    index: 1
    index: 2
  * index: 3

我怎样才能超越它并编写命令来提取类似下面的行?

    index: 0
        ports:
                [Out] HDMI3: HDMI3/DP3 Output (priority 700, latency offset 0 usec, available: no)
    index: 1
        ports:
                [Out] HDMI2: HDMI2/DP2 Output (priority 600, latency offset 0 usec, available: no)
    index: 2
        ports:
                [Out] HDMI1: HDMI1/DP1 Output (priority 500, latency offset 0 usec, available: yes)
  * index: 3
        ports:
                [Out] Speaker: Speaker (priority 100, latency offset 0 usec, available: unknown)
                [Out] Headphones: Headphones (priority 200, latency offset 0 usec, available: yes)

标签: linuxfilterterminal

解决方案


pacmd list-sinks | awk '/index/ { print } /active port:/ { pnt=0 } /ports:/ { print;pnt=1 } pnt==1 && /\[Out\]/ { print }'

打印索引行,然后当我们看到“ports:”时,将变量 pnt 设置为 1,打印该行并跳到下一行。当 pnt 等于 1 并且该行包含“[Out]”时,打印该行。当我们看到“活动端口:”时,将 pnt 变量重置为 0。


推荐阅读