0

如果时间戳匹配,我正在努力寻找一种将下一行附加到当前行的方法。到目前为止,这是我的代码:

open(FH, "error_log:);
@data = <FH>
foreach $line (@data) {
        if ( ($line =~ /notice/)) {  
                $line =~ s/ /,/g;    
                my @L1 = split(/|notice|\[|\]|,mpmstats:,|\t|rdy,|bsy,
+|rd,|wr,|ka,|log,|dns,|cls,|bsy:,|in,|/, $line);
                $line =~ s/|notice|\[|\]|,mpmstats:,|\t|rdy,|bsy,|rd,|
+wr,|ka,|log,|dns,|cls,|bsy:,|in,//g;                    
                print $line;

请注意,我打印只是为了查看输出。输出如下:

Wed,Jun,13,10:40:35,2012,758,42,0,29,11,0,0,2
Wed,Jun,13,10:40:35,2012,29,mod_was_ap22_http.c
Wed,Jun,13,10:41:35,2012,761,39,0,34,5,0,0,0
Wed,Jun,13,10:41:35,2012,34,mod_was_ap22_http.c
Wed,Jun,13,10:42:35,2012,769,31,0,22,6,0,0,3
Wed,Jun,13,10:42:35,2012,22,mod_was_ap22_http.c
Wed,Jun,13,10:43:35,2012,754,46,0,29,17,0,0,0

我希望将数字(第 2 行的 29)以 csv 形式放置在与时间戳对应的第一行的其他数字之后。可以删除该行的其余部分。如果该行下面没有任何内容(例如最后一行),我想附加一个零。感谢您的帮助。

这是所要求的输入数据的一部分:

[Wed Jun 13 01:41:24 2012    [error  [client 10.119.84.9     File does not exist: /ebiz/b2b/IHS70prd/htdocs/offline.b2bonline.html
[Wed Jun 13 01:41:25 2012    [error  [client 10.119.84.9     File does not exist: /ebiz/b2b/IHS70prd/htdocs/offline.b2bonline.html
[Wed Jun 13 01:41:25 2012    [error  [client 10.119.84.8     File does not exist: /ebiz/b2b/IHS70prd/htdocs/offline.b2bonline.html
[Wed Jun 13 01:41:28 2012    [error  [client 10.119.116.8    File does not exist: /ebiz/b2b/IHS70prd/htdocs/offline.b2bonline.html
[Wed Jun 13 01:41:28 2012    [error  [client 10.119.84.8     File does not exist: /ebiz/b2b/IHS70prd/htdocs/offline.b2bonline.html
[Wed Jun 13 01:41:34 2012    [notice     mpmstats: rdy 786 bsy 14 rd 0 wr 11 ka 3 log 0 dns 0 cls 0 
[Wed Jun 13 01:41:34 2012    [notice     mpmstats: bsy: 11 in mod_was_ap22_http.c   
[Wed Jun 13 01:41:34 2012    [error  [client 10.119.84.9     File does not exist: /ebiz/b2b/IHS70prd/htdocs/offline.b2bonline.html
[Wed Jun 13 01:41:35 2012    [error  [client 10.119.84.9     File does not exist: /ebiz/b2b/IHS70prd/htdocs/offline.b2bonline.html
4

2 回答 2

1

你的输入很奇怪。通常,我会看到匹配的方括号。

除此之外,你想要的是这样的:

# This assumes you have Perl 5.10 or autodie installed: failures in open, readline, 
# or close will die automatically
use autodie;

# chunks of your input to ignore, see below...
my %ignorables = map { $_ => 1 } qw(
    [notice mpmstats: rdy bsy rd wr ka log dns cls bsy: in
);

# 3-arg open is safer than 2, lexical my $fh better than a global FH glob
open my $error_fh, '<', 'error_log'; 

# Iterates over the lines in the file, putting each into $_
while (<$error_fh>) {

    # Only worry about the lines containing [notice
    if (/\[notice/) {

        # Split the line into fields, separated by spaces, skip the %ignorables
        my @line = grep { not defined $ignorables{$_} } split /\s+/;

        # More cleanup
        s/^\[//g for @line; # remove [ from [foo

        # Output the line
        print join(",", @line);

        # Assuming the second line always has "in" in it, 
        # but this could be whatever condition that fits your data...
        if (/\bin\b/) {  # \b matches word edges, e.g., avoids matching "glint"
            print "\n";
        }
        else {
            print ",";
        }
    }
}

close $error_fh;

我没有编译这个,所以我不能保证我没有在某个地方打错字。

这里的关键是你第一个print没有换行符,但以逗号结尾。然后,当您检测到这是第二行时添加换行符。

您可以改为@line在循环外部声明并使用它来累积字段,直到您需要在末尾使用换行符输出它们。

于 2012-07-02T19:02:42.497 回答
0

一种使用方式perl。它省略了不包含[notice . 对于匹配的每一行,它会增加一个变量并将不同的字段保存在数组中,具体取决于它是奇数还是偶数([notice的第一次出现或第二次出现)。

perl -ane '
    next unless $F[5] eq q|[notice|;
    ++$notice;
    if ( $notice % 2 != 0 ) {
        push @data, @F[0..4, 8, 10, 12, 14, 16, 18, 20, 22];
        next unless eof;
    }

    push @data, (eof) ? 0 : $F[8];
    $data[0] =~ s/\A\[//;
    printf qq|%s\n|, join q|,|, @data;
    @data = ();
' infile

假设infile您的问题有内容,输出将是:

Wed,Jun,13,01:41:34,2012,786,14,0,11,3,0,0,0,11
于 2012-07-02T19:03:49.453 回答