我有正则表达式的一知半解,所以我想知道,如果你只能选择由SOX程序返回此文本的最大振幅值?在这种情况下,例如,我想只有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
perl -nle 'm{Maximum amplitude:\s+(-?\d?\.?\d+)} and print $1' file
0.712219
该处理与负号和数字,那些没有数字,后面接着一个小数点和任意数量的数字。
我会做在AWK:
awk '/Maximum amplitude:/ { print $NF }' infile
这个滤波器通过匹配Maximum amplitude:
线,然后打印该行的最后一个空间分隔的字段。
只打印有匹配的行,并修改该行。
sed -n '/Maximum amplitude/ s/.* //p' inputfile
^Maximum amplitude:\s*(-?\d\.\d+)\n
见here
^
代表在行的开始。Maximum amplitude:
确保你有正确的路线\s*
零个或多个空格字符(
)
代表捕获组 - 你想正则表达式来什么样的回报-?
任选指减号\d
表示一个单一的数字\.
代表小数位\d+
表示1个或多个数字\n
代表换行符 - 导致换行。这是生成的Perl的regex101.com:
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 $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... 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