Select 正则表达式值

Select value with regular expression

我对正则表达式了解不多,所以想知道你是否只能select sox 程序返回的这段文本中的最大幅度值? 在这种情况下,例如,我想 select 只有 0.712219

Samples read:           6615000
Length (seconds):     75.000000
Scaled by:         2147483647.0
Maximum amplitude:     0.712219
Minimum amplitude:    -0.805969
Midline amplitude:    -0.046875
Mean    norm:          0.009264
Mean    amplitude:    -0.000027
RMS     amplitude:     0.043011
Maximum delta:         0.734100
Minimum delta:         0.000000
Mean    delta:         0.008353
RMS     delta:         0.041470
Rough   frequency:         6767
Volume adjustment:        1.241

^Maximum amplitude:\s*(-?\d\.\d+)\n

参见 here

  • ^ 表示行首。
  • Maximum amplitude: 确保你有正确的行
  • \s* 零个或多个白色 space 个字符
  • ( ) 代表一个捕获组 - 你想要正则表达式到 return
  • -?表示可选的减号
  • \d代表一个数字
  • \.代表小数位
  • \d+表示1个或多个数字
  • \n 表示换行符 - 导致换行符。

这是 regex101.com 生成的 Perl:

use strict;

my $str = 'Samples read:           6615000
Length (seconds):     75.000000
Scaled by:         2147483647.0
Maximum amplitude:     0.712219
Minimum amplitude:    -0.805969
Midline amplitude:    -0.046875
Mean    norm:          0.009264
Mean    amplitude:    -0.000027
RMS     amplitude:     0.043011
Maximum delta:         0.734100
Minimum delta:         0.000000
Mean    delta:         0.008353
RMS     delta:         0.041470
Rough   frequency:         6767
Volume adjustment:        1.241';
my $regex = qr/^Maximum amplitude:\s*(-?\d\.\d+)\n/mp;

if ( $str =~ /$regex/g ) {
  print "Whole match is ${^MATCH} and its start/end positions can be obtained via $-[0] and $+[0]\n";
  # print "Capture Group 1 is  and its start/end positions can be obtained via $-[1] and $+[1]\n";
  # print "Capture Group 2 is  ... and so on\n";
}

# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}

Here 是使用 amp.

命名捕获组的示例
^Maximum amplitude:\s*(?P<amp>-?\d\.\d+)\n
perl -nle 'm{Maximum amplitude:\s+(-?\d?\.?\d+)} and print ' file

0.712219

这会处理带有负号的数字以及带有或不带小数点和任意位数的数字。

我会用 awk 做:

awk '/Maximum amplitude:/ { print $NF }' infile

这按匹配 Maximum amplitude: 的行过滤,然后打印该行的最后一个 space 分隔字段。

只打印匹配的行并修改该行。

sed -n '/Maximum amplitude/ s/.* //p' inputfile