我想在shell命令中解析下面的输出(从ddcutil):
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“值,以便将它们传输到另一个脚本:
11: HDMI-1
12: HDMI-2
0f: DisplayPort-1
10: DisplayPort-2有什么优雅的方法吗?我想出了下面的准则,但我认为一定有更好的方法:
$ sudo ddcutil capabilities | pcregrep -M -o2 "(?s)(Feature: 60.*?Values:)(.*?)(Feature)"如果输出是用json表示的,我可以使用jq,我认为yaml有yq,但我不能让它接受为yaml。
发布于 2023-03-05 14:36:25
#!/bin/perl
while(<>) {
if (/Feature: 60/) {
while(<>) {
last if (/Feature/);
next if (/Values/);
print $_, "\n";
}
}
}容易:)
发布于 2023-03-06 06:58:06
使用sed
$ 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-2https://unix.stackexchange.com/questions/738723
复制相似问题