1

我会接受任何解释语言 Perl、Python、Bash 等。但我更喜欢 Perl,因为这是我想要学习的。我有一个时间戳列表,例如:

17:31:16
17:31:16
17:31:18
17:31:29

我想找到任意 2 个连续行之间的所有最大间隔(前 5 个),并返回时间戳和行号。基本上这是一个软件构建的日志文件,我试图确定哪些步骤花费的时间最长。我给出的示例实际上已经被过滤,行实际上看起来像: [15:57:42]: CC net/sunrpc/xprtsock.o 如果你能给我一个解析该格式的程序,它会更容易一些,然后返回时间差异最大的行号。

这就是我用来从日志中隔离时间戳的方法

   perl -lane 'print $1 if $_ =~ /^\[(\d+:\d+:\d+)\]:*/'

我想要实现的输出类型类似于:

 line 574 20:04:54
 line 575 20:24:55
 Difference 00:20:01

如果您不想解决问题,我很乐意看到一些伪代码或得到任何建议。我已经花时间并且没有有用的代码来显示它。

4

2 回答 2

2

我会稍微升级您的时间匹配正则表达式,以分别捕获时间的组成部分。我们是否必须担心在午夜之前开始构建并运行到第二天凌晨?

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

my $oldtime = "";  # hh:mm:ss for end of long interval
my $oldlineno = 0; # line number in the file of second line
my $oldoffset = 0; # offset in seconds from midnight of second command
my $olddiff = 0;   # time taken for longest command

sub hhmmss
{
    my($time) = @_;
    my(@tm) = (int($time/3600), int($time/60)%60, $time%60);
    return @tm;
}

while (<>)
{
    chomp;
    next unless m/^((\d\d):(\d\d):(\d\d))\s+/;
    my $newoffset = (($2 * 60) + $3) * 60 + $4;
    if ($oldoffset == 0)
    {
        $oldtime = $1;
        $olddiff = 0;
        $oldoffset = $newoffset;
        $oldlineno = $.;
    }
    elsif (($newoffset - $oldoffset) > $olddiff)
    {
        $oldtime = $1;
        $olddiff = $newoffset - $oldoffset;
        $oldoffset = $newoffset;
        $oldlineno = $.;
    }
}

if ($oldoffset != 0)
{
    my $prvlineno = $oldlineno - 1;
    my $newoffset = $oldoffset - $olddiff;
    my(@tm) = hhmmss($newoffset);
    printf "line $prvlineno: %.2d:%.2d:%.2d\n", $tm[0], $tm[1], $tm[2];
    print  "line $oldlineno: $oldtime\n";
    @tm = hhmmss($olddiff);
    printf "diff:   %.2d:%.2d:%.2d\n", $tm[0], $tm[1], $tm[2];
}

给定数据文件 ( data) 和上面的脚本 ( dt.pl):

17:31:16 line1
17:31:18 line2
17:31:29 line3
17:33:59 line4
18:00:21 line5
18:21:03 line6
18:41:25 line7
19:51:54 line8
19:52:34 line9

下面的 scriptlet 产生如下所示的输出:

$ for i in $(seq 1 9); do sed ${i}q data | perl dt.pl; done | so
line 0: 17:31:16
line 1: 17:31:16
diff:   00:00:00
line 1: 17:31:16
line 2: 17:31:18
diff:   00:00:02
line 2: 17:31:18
line 3: 17:31:29
diff:   00:00:11
line 3: 17:31:29
line 4: 17:33:59
diff:   00:02:30
line 4: 17:33:59
line 5: 18:00:21
diff:   00:26:22
line 4: 17:33:59
line 5: 18:00:21
diff:   00:26:22
line 6: 18:00:21
line 7: 18:41:25
diff:   00:41:04
line 7: 18:41:25
line 8: 19:51:54
diff:   01:10:29
line 7: 18:41:25
line 8: 19:51:54
diff:   01:10:29
$

我很想听听您在编写任何代码之前是如何考虑这个问题的。

这显然是一个问题,需要记录前一行信息的(相关部分)以计算它与当前行之间的差异。您还需要保持当前的最大差异,直到您阅读第二个匹配行才能正式确定。这推动了设计。代码中的大重复可以通过无条件地分配 3 个值和$olddiff有条件地分配第四个 ( ) 来减少。在那之后,它主要是一个机械和战术的问题。

像这样跨多行匹配是一个麻烦的过程;你必须处理保持适当的状态。部分是经验问题;这种事情你做了几十次之后,下一次就不需要那么长时间了。

于 2013-09-24T20:13:41.777 回答
1

只求最大的不同

perl -l -n -e 'BEGIN {$m=0;$last=0;$am=$.;} /(\d+):(\d+):(\d+)/; $v=($1*3600)+($2*60)+$3;   if ($last && $v-$last > $m) { $am=$.; $m=$v-$last;} $last=$v; END { print "max diff ",$m, " at line $am\n" }' d.txt

前5

perl -l -n -e 'BEGIN {%h=();$last=0;} /(\d+):(\d+):(\d+)/; $v=($1*3600)+($2*60)+$3;   if ($last) { $h{$v-$last}=$.;} $last=$v; END { for ((sort {$b <=> $a} keys %h)[0..4]) { print "line ",$h{$_}," $_"; }}' d.txt

在蟒蛇

last = 0
list = []
linenumber = 1
for t in (open("d.txt","r").readlines()):
    q=3600
    v=0
    for x in t.split(":"):
        v = v + q*int(x)
        q = q / 60
    if (last >0):
      list.append([v-last, linenumber])
    last = v
    linenumber = linenumber + 1

top = sorted(list, key=lambda n: n[0], reverse=True)[0:5]
print top
于 2013-09-24T20:00:33.310 回答