Convert column to matrix format using awk

前端 未结 4 2109
有刺的猬
有刺的猬 2021-02-15 16:36

I have a gridded data file in column format as:

ifile.txt
x     y     value
20.5  20.5  -4.1
21.5  20.5  -6.2
22.5  20.5   0.0
20.5  21.5   1.2
21.5  21.5   4.3
         


        
4条回答
  •  鱼传尺愫
    2021-02-15 17:11

    Perl solution:

    #!/usr/bin/perl -an
    $h{ $F[0] }{ $F[1] } = $F[2] unless 1 == $.;
    END {
        @s = sort { $a <=> $b } keys %h;
        print ' ' x 5;
        printf '%5.1f' x @s, @s;
        print "\n";
        for my $u (@s) {
            print "$u ";
            printf '%5.1f', $h{$u}{$_} for @s;
            print "\n";
        }
    }
    
    • -n reads the input line by line
    • -a splits each line on whitespace into the @F array
    • See sort, print, printf, and keys.

提交回复
热议问题