我有以下问题,也许你可以提供帮助:
我想要匹配的文字是这样的:
Data Generated using Turbine's method
Stuff
more Stuff
Full speed : 0.87
Data generated using My method
Stuff
more stuff
Full speed : 0.96
Data Generated using Turbine's method
Stuff
more Stuff
Full speed : 0.83
Data generated using My method
Stuff
more stuff
Full speed : 0.94
我希望匹配包含全速的行并将它们输出到这样的表中:
Turbine's My
0.87 0.96
0.83 0.94
所以我可以比较两种方法。但是我无法获得与当前正则表达式匹配的awk:
/Data Generated using Turbine's method.*Full speed/
/Data Generated using My method.*Full speed/
我的问题到底是什么?为什么awk不符合这个?
感谢您的建议
答案 0 :(得分:3)
AWK中的单个RE仅尝试匹配单行。您似乎想要一个范围模式,例如:/^Data Generated/, /^Full Speed.*$/
。
编辑:准确地获取您要求的格式相对很难。如果你不介意将它转向侧面,可以这么说,所以每一组都在一条线而不是一列,它变得相当简单:
/^Data/ { name = $4; }
/^Full/ { speeds[name] = speeds[name] " " $4; }
END {
for (i in speeds)
printf("%10s : %s\n", i, speeds[i]);
}
答案 1 :(得分:2)
试试这个:
awk -F: 'BEGIN {OFS="\t"; print "Turbine\047s" OFS "My"} /Turbine/ {tflag=1; mflag=0} /My/ {mflag=1; tflag=0} /Full speed/ {if (tflag) {T=$2; tflag=0}; if (mflag) { print T OFS OFS $2; mflag=0}}' inputfile
在不同的行上:
awk -F: 'BEGIN {OFS="\t"; print "Turbine\047s" OFS "My"}
/Turbine/ {tflag=1; mflag=0}
/My/ {mflag=1; tflag=0}
/Full speed/ {
if (tflag) {T=$2; tflag=0};
if (mflag) { print T OFS OFS $2; mflag=0}}' inputfile
或稍微简单的版本:
awk -F: '/Turbine/, /^Full speed/ {if ($0 ~ /Full/) T=$2}
/My/, /^Full speed/ {if ($0 ~ /Full/) print T, $2}'
答案 2 :(得分:0)
我使用Perl:
perl -ne '
if (/(\S+) method/) {$method = $1}
if (/Full speed : ([\d.]+)/) {push @{$speeds{$method}}, $1}
END {
@keys = keys %speeds;
print join("\t", @keys), "\n";
$max = 0;
for $v (values %speeds) {
$len = scalar @$v;
$max = $len if $len > $max;
}
for $i (0 .. $max-1) {
for $k (@keys) {print $speeds{$k}[$i], "\t"};
print "\n";
}
}
' speed.txt
输出
My Turbine's
0.96 0.87
0.94 0.83