开发者

awk catch text going into the next line

开发者 https://www.devze.com 2023-01-23 07:59 出处:网络
i have the following problem, maybe you can help out: The text i want to match is like this : Data Generated using Turbine\'s method

i have the following problem, maybe you can help out:

The text i want to match is like this :

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

I want to match the lines containing full speed and output them into a table like this:

Turbine's My
0.87    0.96
0.83    0.94开发者_开发知识库

so i can compare the two methods. However i have trouble getting awk to match my current regex which is:

/Data Generated using Turbine's method.*Full speed/
/Data Generated using My method.*Full speed/

What is my problem exactly? Why doesn't awk match this?

thanks for the advice


A single RE in AWK only attempts to match against a single line. You seem to want a range pattern, something like: /^Data Generated/, /^Full Speed.*$/.

Edit: getting exactly the format you've asked for is relatively difficult. If you don't mind turning it sideways, so to speak, so each set is on a line instead of in a column, it becomes rather simpler:

/^Data/     { name = $4; }
/^Full/     { speeds[name] = speeds[name] " " $4; } 

END { 
    for (i in speeds)
        printf("%10s : %s\n", i, speeds[i]);
}


Try this:

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

On separate lines:

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

Or a slightly simpler version:

awk -F: '/Turbine/, /^Full speed/ {if ($0 ~ /Full/) T=$2}
         /My/, /^Full speed/ {if ($0 ~ /Full/) print T, $2}'


I'd use 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

which outputs

My      Turbine's
0.96    0.87
0.94    0.83
0

精彩评论

暂无评论...
验证码 换一张
取 消