Perl加载到一系列正则表达式之间的数组中

时间:2011-11-04 02:52:43

标签: perl

所以我有这个日志文件 - 它很长。
我真正需要加载到数组中的是从end\s'\w+'\soutputSUCCESS的行 我想用这两个参数之间的信息加载数组,并抽出名称(例如SENDALLLINES)最后列出的时间(例如07:13:56)和数字(例如399)。
我不知道如何让数组在包含SUCCESS的行停止加载,这样我就可以操作信息了。

end 'SENDALLMARKETS' output
07:13:46 /data/noc/startup/send_spin.sh successfully spun out SENDALLMARKETS
07:13:46 /data/noc/startup/send_spins.sh Sleeping for 5 seconds so system can catch up.
07:13:51 /data/noc/startup/send_spin.sh pats0028/PSX_TRMASTER sending spin SENDALLLINES
07:13:51 /data/noc/startup/send_spin.sh start 'SENDALLLINES' output:
SUCCESS: Line Range spins sequenced to core stream

end 'SENDALLLINES' output
07:13:51 /data/noc/startup/send_spin.sh successfully spun out SENDALLLINES
07:13:51 /data/noc/startup/send_spins.sh Sleeping for 5 seconds so system can catch up.
07:13:56 /data/noc/startup/send_spins.sh finished sending spins
07:13:56 /data/noc/startup/check_tradereporters.sh all trade reporting lines are     connected and logged in
07:13:56 /data/noc/startup/send_spins.sh pats0010/SPINMASTER sending spins S    ENDALLACCOUNTS SENDALLFIRMS SENDALLAIQ SENDALLSTOCKS SENDALLATTRIBUTABLE
07:13:56 /data/noc/startup/send_spin.sh pats0010/SPINMASTER sending spin  SENDALLACCOUNTS
07:13:56 /data/noc/startup/send_spin.sh start 'SENDALLACCOUNTS' output:
SUCCESS: 399 account record(s) sent

end 'SENDALLACCOUNTS' output
07:13:56 /data/noc/startup/send_spin.sh successfully spun out SENDALLACCOUNTS
07:13:56 /data/noc/startup/send_spins.sh Sleeping for 5 seconds so system can catch up.
07:14:01 /data/noc/startup/send_spin.sh pats0010/SPINMASTER sending spin SENDALLFIRMS
07:14:02 /data/noc/startup/send_spin.sh start 'SENDALLFIRMS' output:
SUCCESS: 1488 firm record(s) sent
  

这就是我到目前为止所做的一切

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

my $LogDir = "/home/shortcasper/perl/work_perl_short/";
my $logFile = "initstart";
my @Log_array;
open ( my $FILE, '<', "$LogDir$logFile") or die ("could not open the file -- $!")
while (<$FILE>) {   
        my $line = $_;
        next if ($line =~ /^$/);
        if ($line =~ /end\s\'\w+\'\soutput/) {
                push (@Log_array, $line);
        }
        foreach my $logLine(@Log_array){
                print $logLine;
        }
        @Log_array = ();
}

2 个答案:

答案 0 :(得分:3)

range operator是你的朋友。

假设Perl≥5.10,类似

while (my $line = readline $FILE) {
    if ($line =~ /end\s'(\w+)'\soutput/ ... $line =~ /SUCCESS/) {
        $name //= $1;
        push @lines, $line;
    } elsif (defined $name) {
        say "Got $#lines for $name";
        undef $name;
        @lines = ();
    }
}

答案 1 :(得分:2)

这就是你想要的吗?

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

my ($name, $time, $count);
while(<DATA>) {
    chomp;
    if (/^end '(.+?)'/) {
        $name = $1;
    } elsif (/^SUCCESS: (\w+)/) {
        $count = $1;
        $count = 0 if ($count =~ /^\D+$/);
        print "name: $name, time: $time, count: $count\n";
    } else {
        ($time) = $_ =~ /^(\d\d:\d\d:\d\d)/;
    }
}

__DATA__
end 'SENDALLMARKETS' output
07:13:46 /data/noc/startup/send_spin.sh successfully spun out SENDALLMARKETS
07:13:46 /data/noc/startup/send_spins.sh Sleeping for 5 seconds so system can catch up.
07:13:51 /data/noc/startup/send_spin.sh pats0028/PSX_TRMASTER sending spin SENDALLLINES
07:13:51 /data/noc/startup/send_spin.sh start 'SENDALLLINES' output:
SUCCESS: Line Range spins sequenced to core stream

end 'SENDALLLINES' output
07:13:51 /data/noc/startup/send_spin.sh successfully spun out SENDALLLINES
07:13:51 /data/noc/startup/send_spins.sh Sleeping for 5 seconds so system can catch up.
07:13:56 /data/noc/startup/send_spins.sh finished sending spins
07:13:56 /data/noc/startup/check_tradereporters.sh all trade reporting lines are     connected and logged in
07:13:56 /data/noc/startup/send_spins.sh pats0010/SPINMASTER sending spins S    ENDALLACCOUNTS SENDALLFIRMS SENDALLAIQ SENDALLSTOCKS SENDALLATTRIBUTABLE
07:13:56 /data/noc/startup/send_spin.sh pats0010/SPINMASTER sending spin  SENDALLACCOUNTS
07:13:56 /data/noc/startup/send_spin.sh start 'SENDALLACCOUNTS' output:
SUCCESS: 399 account record(s) sent

end 'SENDALLACCOUNTS' output
07:13:56 /data/noc/startup/send_spin.sh successfully spun out SENDALLACCOUNTS
07:13:56 /data/noc/startup/send_spins.sh Sleeping for 5 seconds so system can catch up.
07:14:01 /data/noc/startup/send_spin.sh pats0010/SPINMASTER sending spin SENDALLFIRMS
07:14:02 /data/noc/startup/send_spin.sh start 'SENDALLFIRMS' output:
SUCCESS: 1488 firm record(s) sent

<强>输出:

name: SENDALLMARKETS, time: 07:13:51, count: 0
name: SENDALLLINES, time: 07:13:56, count: 399
name: SENDALLACCOUNTS, time: 07:14:02, count: 1488