Thread Zeilenweises einlesen und print bei fund eines Strings (6 answers)
Opened by Fragensteller at 2013-09-03 14:30

pq
 2013-09-03 15:39
#169887 #169887
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
sind die zeilen im log nicht chronologisch?
wenn nicht, dann schau dir mal den code an:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use strict;
use warnings;
use Time::Local;
use 5.010;

my %seen;
my %months = (
    Jan => 1,
    Feb => 2,
    Mar => 3,
    Apr => 4,
    May => 5,
    Jun => 6,
    Jul => 7,
    Aug => 8,
    Sep => 9,
    Oct => 10,
    Nov => 11,
    Dec => 12,
);
open my $fh, "<", "phperrors.log" or die $!;
my @lines;
while (my $line = <$fh>) {
    chomp $line;
    if ($line =~ /error/) {
        unless ($seen{ $line }++) {
            if ($line =~ m/^\[(\d{2})-(\w{3})-(\d{4}) (\d{2}):(\d{2}):(\d{2})\]/) {
                my ($day, $monthname, $year, $hour, $minute, $sec) = ($1, $2, $3, $4, $5, $6);
                my $month = $months{ $monthname } - 1;
                my $epoch = timelocal($sec, $minute, $hour, $day, $month, $year);

                push @lines, [$epoch, $line];
            }
        }
    }
}

@lines = map {
    $_->[1]
} sort {
    $a->[0] <=> $b->[0]
} @lines;

say for @lines;

du speicherst die epochsekunden mit im array. danach kannst du dann einfach sortieren und am ende die epochsekunden mit dem map wieder rauswerfen.
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread Zeilenweises einlesen und print bei fund eines Strings