首页 > 解决方案 > grep 命令输出,但如何查找特定块?

问题描述

我正在寻找一些带有 grep 的特定块

例如,我有来自 android 设备的输出:

Stream volumes (device: index)
- STREAM_VOICE_CALL:
   Muted: false
   Min: 1
   Max: 5
   Current: 40000000 (default): 4
   Devices: earpiece
- STREAM_SYSTEM:
   Muted: false
   Min: 0
   Max: 7
   Current: 40000000 (default): 5
   Devices: speaker
- STREAM_RING:
   Muted: false
   Min: 0
   Max: 7
   Current: 40000000 (default): 5
   Devices: speaker
**- STREAM_MUSIC:
   Muted: false
   Min: 0
   Max: 15
   Current: 2 (speaker): 12, 4000000 (usb_headset): 3, 40000000 (default): 8
   Devices: speaker**
- STREAM_ALARM:
   Muted: false
   Min: 0
   Max: 7
   Current: 40000000 (default): 6
   Devices: speaker
- STREAM_NOTIFICATION:
   Muted: false
   Min: 0
   Max: 7
   Current: 40000000 (default): 5
   Devices: speaker
- STREAM_BLUETOOTH_SCO:
   Muted: false
   Min: 0
   Max: 15
   Current: 40000000 (default): 7
   Devices: earpiece
- STREAM_SYSTEM_ENFORCED:
   Muted: false
   Min: 0
   Max: 7
   Current: 40000000 (default): 5
   Devices: speaker
- STREAM_DTMF:
   Muted: false
   Min: 0
   Max: 15
   Current: 40000000 (default): 11
   Devices: speaker
- STREAM_TTS:
   Muted: false
   Min: 0
   Max: 15
   Current: 2 (speaker): 12, 4000000 (usb_headset): 3, 40000000 (default): 8
   Devices: speaker
- STREAM_ACCESSIBILITY:
   Muted: false
   Min: 0
   Max: 15
   Current: 2 (speaker): 12, 4000000 (usb_headset): 3, 40000000 (default): 8
   Devices: speaker

我需要使用 grep 获取 ** ** 中的块,我需要哪个代码 grep 命令来找到特定的输出块?我试过了

adb shell dumpsys 音频| grep {STREAM_MUSIC:,STREAM_ALARM} 并且不返回任何内容 adb shell dumpsys audio | grep -w STREAM_MUSIC 只返回第一行

标签: grep

解决方案


如果你可以使用awk,你可以这样做:

awk '/- STREAM/ {f=0} /- STREAM_MUSIC:/ {f=1} f'
- STREAM_MUSIC:
   Muted: false
   Min: 0
   Max: 15
   Current: 2 (speaker): 12, 4000000 (usb_headset): 3, 40000000 (default): 8
   Devices: speaker

推荐阅读