首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从缩进输出中提取行

从缩进输出中提取行
EN

Unix & Linux用户
提问于 2023-03-05 13:17:28
回答 2查看 96关注 0票数 2

我想在shell命令中解析下面的输出(从ddcutil):

代码语言:javascript
复制
Model: MQ780
MCCS version: 2.1
Commands:
   Op Code: 01 (VCP Request)
   Op Code: 02 (VCP Response)
   Op Code: 03 (VCP Set)
   Op Code: 0C (Save Settings)
   Op Code: E3 (Capabilities Reply)
   Op Code: F3 (Capabilities Request)
VCP Features:
   Feature: 02 (New control value)
   Feature: 04 (Restore factory defaults)
   Feature: 05 (Restore factory brightness/contrast defaults)
   Feature: 08 (Restore color defaults)
   Feature: 10 (Brightness)
   Feature: 12 (Contrast)
   Feature: 14 (Select color preset)
      Values:
         05: 6500 K
         08: 9300 K
         0b: User 1
   Feature: 16 (Video gain: Red)
   Feature: 18 (Video gain: Green)
   Feature: 1A (Video gain: Blue)
   Feature: 52 (Active control)
   Feature: 60 (Input Source)
      Values:
         11: HDMI-1
         12: HDMI-2
         0f: DisplayPort-1
         10: DisplayPort-2
   Feature: AC (Horizontal frequency)
   Feature: AE (Vertical frequency)
   Feature: B2 (Flat panel sub-pixel layout)
   Feature: B6 (Display technology type)
   Feature: C0 (Display usage time)
   Feature: C6 (Application enable key)
   Feature: C8 (Display controller type)
   Feature: C9 (Display firmware level)
   Feature: D6 (Power mode)
      Values:
         01: DPM: On,  DPMS: Off
         04: DPM: Off, DPMS: Off

我正在尝试提取"Feature: 60“值,以便将它们传输到另一个脚本:

代码语言:javascript
复制
11: HDMI-1
12: HDMI-2
0f: DisplayPort-1
10: DisplayPort-2

有什么优雅的方法吗?我想出了下面的准则,但我认为一定有更好的方法:

代码语言:javascript
复制
$ sudo ddcutil capabilities | pcregrep -M -o2 "(?s)(Feature: 60.*?Values:)(.*?)(Feature)"

如果输出是用json表示的,我可以使用jq,我认为yaml有yq,但我不能让它接受为yaml。

EN

回答 2

Unix & Linux用户

发布于 2023-03-05 14:36:25

代码语言:javascript
复制
#!/bin/perl
while(<>) {
   if (/Feature: 60/) {
     while(<>) {
       last if (/Feature/);
       next if (/Values/);
       print $_, "\n";
     }
   }
}

容易:)

票数 3
EN

Unix & Linux用户

发布于 2023-03-06 06:58:06

使用sed

代码语言:javascript
复制
$ sudo ddcutil capabilities | sed -En '/Feature: 60/{n;/Values:/{:a;n;/Feature:/,/Feature:/!s/^[ \t]+//gp;ba}}'
11: HDMI-1
12: HDMI-2
0f: DisplayPort-1
10: DisplayPort-2
票数 1
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/738723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档