我正在编写一个列出所有具有pvolume功能的alsa设备的正则表达式 基本上是:
amixer | perl -0pe "s/Simple mixer control '(.*)',(.*)\n Capabilities: .*pvolume.*\n( .*\n)*( (.*): Playback .*\[([0-9]+)%\].*\[(on|off)\]\n)/\2 - \1 (Vol. \6) (\7)\n/g"
但是这会打印出无法匹配的群组。
我尝试使用“-0ne”和“print if s / ...”但它仍然是相同的。
我怎样才能制作它只打印我想要的那些?
Amixer输出:
Simple mixer control 'Master',0
Capabilities: pvolume pvolume-joined pswitch pswitch-joined
Playback channels: Mono
Limits: Playback 0 - 87
Mono: Playback 81 [93%] [-4.50dB] [on]
Simple mixer control 'Headphone',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 87
Mono:
Front Left: Playback 87 [100%] [0.00dB] [on]
Front Right: Playback 87 [100%] [0.00dB] [on]
Simple mixer control 'Speaker',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 87
Mono:
Front Left: Playback 71 [82%] [-12.00dB] [on]
Front Right: Playback 71 [82%] [-12.00dB] [on]
Simple mixer control 'Bass Speaker',0
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [off]
Simple mixer control 'PCM',0
Capabilities: pvolume
Playback channels: Front Left - Front Right
Limits: Playback 0 - 255
Mono:
Front Left: Playback 255 [100%] [0.00dB]
Front Right: Playback 255 [100%] [0.00dB]
Simple mixer control 'Mic',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 31
Mono:
Front Left: Playback 31 [100%] [12.00dB] [off]
Front Right: Playback 31 [100%] [12.00dB] [off]
Simple mixer control 'Mic Boost',0
Capabilities: volume
Playback channels: Front Left - Front Right
Capture channels: Front Left - Front Right
Limits: 0 - 3
Front Left: 0 [0%] [0.00dB]
Front Right: 0 [0%] [0.00dB]
Simple mixer control 'IEC958',0
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [off]
Simple mixer control 'IEC958 Default PCM',0
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [on]
Simple mixer control 'Capture',0
Capabilities: cvolume cswitch
Capture channels: Front Left - Front Right
Limits: Capture 0 - 31
Front Left: Capture 26 [84%] [22.50dB] [on]
Front Right: Capture 26 [84%] [22.50dB] [on]
Simple mixer control 'Auto-Mute Mode',0
Capabilities: enum
Items: 'Disabled' 'Enabled'
Item0: 'Disabled'
Simple mixer control 'Digital',0
Capabilities: cvolume
Capture channels: Front Left - Front Right
Limits: Capture 0 - 120
Front Left: Capture 87 [72%] [13.50dB]
Front Right: Capture 87 [72%] [13.50dB]
Simple mixer control 'Internal Mic',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 31
Mono:
Front Left: Playback 23 [74%] [0.00dB] [off]
Front Right: Playback 23 [74%] [0.00dB] [off]
Simple mixer control 'Internal Mic Boost',0
Capabilities: volume
Playback channels: Front Left - Front Right
Capture channels: Front Left - Front Right
Limits: 0 - 3
Front Left: 0 [0%] [0.00dB]
Front Right: 0 [0%] [0.00dB]
输出我的命令(使用标志-n和“print if”不会改变输出)
0 - Master (Vol. 93) (on)
0 - Headphone (Vol. 100) (on)
0 - Speaker (Vol. 82) (on)
Simple mixer control 'Bass Speaker',0
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [off]
Simple mixer control 'PCM',0
Capabilities: pvolume
Playback channels: Front Left - Front Right
Limits: Playback 0 - 255
Mono:
Front Left: Playback 255 [100%] [0.00dB]
Front Right: Playback 255 [100%] [0.00dB]
0 - Mic (Vol. 100) (off)
Simple mixer control 'Mic Boost',0
Capabilities: volume
Playback channels: Front Left - Front Right
Capture channels: Front Left - Front Right
Limits: 0 - 3
Front Left: 0 [0%] [0.00dB]
Front Right: 0 [0%] [0.00dB]
Simple mixer control 'IEC958',0
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [off]
Simple mixer control 'IEC958 Default PCM',0
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [on]
Simple mixer control 'Capture',0
Capabilities: cvolume cswitch
Capture channels: Front Left - Front Right
Limits: Capture 0 - 31
Front Left: Capture 26 [84%] [22.50dB] [on]
Front Right: Capture 26 [84%] [22.50dB] [on]
Simple mixer control 'Auto-Mute Mode',0
Capabilities: enum
Items: 'Disabled' 'Enabled'
Item0: 'Disabled'
Simple mixer control 'Digital',0
Capabilities: cvolume
Capture channels: Front Left - Front Right
Limits: Capture 0 - 120
Front Left: Capture 87 [72%] [13.50dB]
Front Right: Capture 87 [72%] [13.50dB]
0 - Internal Mic (Vol. 74) (off)
Simple mixer control 'Internal Mic Boost',0
Capabilities: volume
Playback channels: Front Left - Front Right
Capture channels: Front Left - Front Right
Limits: 0 - 3
Front Left: 0 [0%] [0.00dB]
Front Right: 0 [0%] [0.00dB]
期望的输出:
0 - Master (Vol. 93) (on)
0 - Headphone (Vol. 100) (on)
0 - Speaker (Vol. 82) (on)
0 - Mic (Vol. 100) (off)
0 - Internal Mic (Vol. 74) (off)
答案 0 :(得分:0)
使用-n
标志代替-p
;后者自动打印线条。并且然后添加print语句:例如:
$ cat .zshrc | perl -n -e "print if s/down/hello/g"
结果:
# Up and hello arrows search history matching input so far {{{
autoload hello-line-or-beginning-search
zle -N hello-line-or-beginning-search
bindkey "^[[B" hello-line-or-beginning-search
答案 1 :(得分:0)
这是一个可能的解决方案。使用/ mg选项的
# /^Simple[ ]mixer[ ]control.*'([^']*)',(\d+).*\n[ ]{2}Capabilities:.+pvolume.*\n[ ]{2}Playback.*\n(?:[ ]{2}.*\n)*?[ ]{2}.*\[(\d+)%\].*\[(on|off)\]/
^ Simple [ ] mixer [ ] control .*
'
( [^']* ) # (1)
',
( \d+ ) # (2)
.* \n
[ ]{2} Capabilities: .+ pvolume .* \n
[ ]{2} Playback .* \n
(?: [ ]{2} .* \n )*?
[ ]{2} .*
\[
( \d+ ) # (3)
%\]
.*
\[
( on | off ) # (4)
\]
Perl测试用例
use strict;
use warnings;
$/ = undef;
my $str = <DATA>;
while ($str =~ /^Simple[ ]mixer[ ]control.*'([^']*)',(\d+).*\n[ ]{2}Capabilities:.+pvolume.*\n[ ]{2}Playback.*\n(?:[ ]{2}.*\n)*?[ ]{2}.*\[(\d+)%\].*\[(on|off)\]/mg)
{
print "$2 - $1 (Vol. $3) ($4)\n";
}
__DATA__
Simple mixer control 'Master',0
Capabilities: pvolume pvolume-joined pswitch pswitch-joined
Playback channels: Mono
Limits: Playback 0 - 87
Mono: Playback 81 [93%] [-4.50dB] [on]
Simple mixer control 'Headphone',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 87
Mono:
Front Left: Playback 87 [100%] [0.00dB] [on]
Front Right: Playback 87 [100%] [0.00dB] [on]
Simple mixer control 'Speaker',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 87
Mono:
Front Left: Playback 71 [82%] [-12.00dB] [on]
Front Right: Playback 71 [82%] [-12.00dB] [on]
Simple mixer control 'Bass Speaker',0
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [off]
Simple mixer control 'PCM',0
Capabilities: pvolume
Playback channels: Front Left - Front Right
Limits: Playback 0 - 255
Mono:
Front Left: Playback 255 [100%] [0.00dB]
Front Right: Playback 255 [100%] [0.00dB]
Simple mixer control 'Mic',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 31
Mono:
Front Left: Playback 31 [100%] [12.00dB] [off]
Front Right: Playback 31 [100%] [12.00dB] [off]
Simple mixer control 'Mic Boost',0
Capabilities: volume
Playback channels: Front Left - Front Right
Capture channels: Front Left - Front Right
Limits: 0 - 3
Front Left: 0 [0%] [0.00dB]
Front Right: 0 [0%] [0.00dB]
Simple mixer control 'IEC958',0
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [off]
Simple mixer control 'IEC958 Default PCM',0
Capabilities: pswitch pswitch-joined
Playback channels: Mono
Mono: Playback [on]
Simple mixer control 'Capture',0
Capabilities: cvolume cswitch
Capture channels: Front Left - Front Right
Limits: Capture 0 - 31
Front Left: Capture 26 [84%] [22.50dB] [on]
Front Right: Capture 26 [84%] [22.50dB] [on]
Simple mixer control 'Auto-Mute Mode',0
Capabilities: enum
Items: 'Disabled' 'Enabled'
Item0: 'Disabled'
Simple mixer control 'Digital',0
Capabilities: cvolume
Capture channels: Front Left - Front Right
Limits: Capture 0 - 120
Front Left: Capture 87 [72%] [13.50dB]
Front Right: Capture 87 [72%] [13.50dB]
Simple mixer control 'Internal Mic',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 31
Mono:
Front Left: Playback 23 [74%] [0.00dB] [off]
Front Right: Playback 23 [74%] [0.00dB] [off]
Simple mixer control 'Internal Mic Boost',0
Capabilities: volume
Playback channels: Front Left - Front Right
Capture channels: Front Left - Front Right
Limits: 0 - 3
Front Left: 0 [0%] [0.00dB]
Front Right: 0 [0%] [0.00dB]
输出&gt;&gt;
0 - Master (Vol. 93) (on)
0 - Headphone (Vol. 100) (on)
0 - Speaker (Vol. 82) (on)
0 - Mic (Vol. 100) (off)
0 - Internal Mic (Vol. 74) (off)