总结一个数字列,其中滑动窗口由另一列中的值定义

时间:2011-07-08 21:59:41

标签: linux perl unix awk

我现在面临的问题是将数字列与另一列中的值定义的滑动窗口相加。

(1)我的数据以制表符分隔,有两个数字列:

1000 12
2000 10
3000 9
5000 3
9000 5
10000 90
30000 20
31000 32
39000 33
40000 28

(2)我想用第1列定义的窗口总结第2列,窗口大小是(第1列+3000)。这意味着我需要添加第3列(第3列= sum(第1列的第2列的所有值到第1列+ 3000))。

看起来像这样:

1000 12 12+10+9
2000 10 10+9+3
3000 9 9
5000 3 3
9000 5 5+90
10000 90 90
30000 20 20+32
31000 32 32
39000 33 33
40000 28

(3)我是编程新手。我试过awk,但我失败了。

我不知道如何控制第1列的窗口

awk'(i = 1; i< = NR; i ++){sum [i] + = $ 2} END {print $ 1,$ 2,sum}'mydata

有人能就这个问题给我任何建议/指示吗?提前谢谢。

最佳,

4 个答案:

答案 0 :(得分:3)

我对awk并不是很好,但是这里是我在perl中一起攻击的东西,如果你在unix系统上也应该运行。假设您将其保存为名为window.pl的文件:

#!/usr/bin/perl -w
use strict;

# Usage: window.pl < [filepath or text stream]
# Example: window.pl < window.txt

my $window = 3000;
my @lines = <STDIN>;
my $i = 0;
my $last_line = $#lines;

# Start reading each line
while ($i<= $last_line)
{
    my $current_line = $lines[$i];
    my ($col1, $col2) = ( $current_line =~ /(\d+)\s+(\d+)/ );
    my $ubound = $col1 + $window;
    my @sums = $col2;
    my $lookahead = $i + 1;

    # Start looking at subsequent lines within the window
    while ($lookahead <= $last_line)
    {
        my $next_line = $lines[$lookahead];
        my ($c1, $c2) = ( $next_line =~ /(\d+)\s+(\d+)/ );
        if ($c1 <= $ubound)
        {
            push @sums, $c2;
            ++$lookahead;
        }
        else
        {
            last;
        }
    }

    my $output;
    if ( $#sums > 0 )
    {
        my $sum = join "+", @sums;
        $output = "$col1 $sum\n";
    }
    else
    {
        $output = "$col1 $col2\n";
    }
    print $output;
    ++$i;
}

输出:

1000 12+10+9
2000 10+9+3
3000 9+3
5000 3
9000 5+90
10000 90
30000 20+32
31000 32
39000 33+28
40000 28

仅当输入文件足够小以读入内存时才有效,但这可能会对您有所帮助。

祝你好运!

答案 1 :(得分:3)

这是一个Perl解决方案:

use warnings;
use strict;

my (%data, @ids);
while (<DATA>) { # read in the data
    /^(\d+)\s+(\d+)$/ or die "bad input: $_";
    push @ids, $1;
    $data{$1} = [$2]
}
for (0 .. $#ids) { # slide window over data
    my ($i, $id) = ($_ + 1, $ids[$_]);

    push @{$data{$id}}, $data{ $ids[$i++] }[0]
        while $i < @ids and $ids[$i] <= $id + 3000;
}

$" = '+';                                                               #"
print "$_: @{$data{$_}}\n" for @ids;

__DATA__
1000 12
2000 10
3000 9
5000 3
9000 5
10000 90
30000 20
31000 32
39000 33
40000 28

打印哪些:

1000: 12+10+9
2000: 10+9+3
3000: 9+3
5000: 3
9000: 5+90
10000: 90
30000: 20+32
31000: 32
39000: 33+28
40000: 28

答案 2 :(得分:2)

这并不是真正擅长任何语言的东西,实际上你所要求的是一项相当具有挑战性的编程任务,特别是对于新手而言。

尽管如此,这里有一个awk脚本:

BEGIN {
    window = 3000;
}

function push(line, sum,   n) {
    n = length(lines);
    lines[n] = line;
    sums[n] = sum;
}

function pop(  n, i) {
    n = length(lines);

    if (n > 1) {
        for(i = 0; i < n - 1; i++) {
            lines[i] = lines[i + 1];
            sums[i] = sums[i + 1];
        }
    }
    if (n > 0) {
        delete lines[n - 1];
        delete sums[n - 1];
    }
}

{
    cur_line = $1;
    value = $2;
    n = length(lines);
    pops = 0;
    for (i = 0; i < n; i++) {
        if (lines[i] + window < cur_line) {
            print "Sum for " lines[i] " = " sums[i];
            pops++;
         }
    }
    for (i = 0; i < pops; i++) {
        pop();
    }
    push(cur_line, 0);
    n = length(lines);
    for (i = 0; i < n; i++) {
        sums[i] = sums[i] + value;
    }
}

END {
    n = length(lines);
    for (i = 0; i < n; i++) {
        if (lines[i] < cur_line + window) {
            print "Sum for " lines[i] " = " sums[i];
         }
    }
}

这是对样本数据的一次运行:

Sum for 1000 = 31
Sum for 2000 = 22
Sum for 3000 = 12
Sum for 5000 = 3
Sum for 9000 = 95
Sum for 10000 = 90
Sum for 30000 = 52
Sum for 31000 = 32
Sum for 39000 = 61
Sum for 40000 = 28

答案 3 :(得分:2)

这是一个稍微紧凑的解决方案版本:

#!/usr/bin/perl
use strict;
use warnings;

use constant WIN_SIZE => 3000;

my @pending;

while (<>) {
    my ($pos, $val) = split;

    # Store line info, sum, and when to stop summing
    push @pending, { pos   => $pos,
                     val   => $val,
                     limit => $pos + WIN_SIZE,
                     sum   => 0 };

    show($_)   for grep { $_->{limit} <  $pos } @pending; # Show items beyond window

    @pending =     grep { $_->{limit} >= $pos } @pending; # Keep items still in window

    $_->{sum} += $val for @pending;                       # And continue their sums
}

# and don't forget those items left within the window when the data ran out
show($_) for @pending;

sub show {
    my $pending = shift;
    print join("\t", $pending->{pos}, $pending->{val}, $pending->{sum}), "\n";
}

只需将其放入脚本中,然后在同一行上提供数据文件,例如:

$ perl script.pl mydata
1000    12  31
2000    10  22
3000    9   12
5000    3   3
9000    5   95
10000   90  90
30000   20  52
31000   32  32
39000   33  61
40000   28  28