1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/perl use strict; use warnings; my @test; push @test,'[Thu Nov 08 22:59:19 2012] [error] Hostname www.......de provided via SNI, but no hostname provided in HTTP request'; push @test,'[Fri Nov 09 09:49:30 2012] [error] [client 8.1.1.1] Can\'t modify constant item in scalar assignment at /script.pl line 16, near "\'schiess mich tot\';", referer: https://.......pl'; for (my $z = 0; $z < scalar @test; $z++) { print "Zeile $z: "; if ( $test[$z] =~ /^\[[^ ]+ ([^ ]+) ?([0-9]+) ([0-9:]+) ([0-9]+)\] \[([^\]]+)\] \[[^ ]+ ([^\]]+)\] (.*)$/ ) { print "matcht 7=>$7<\n"; } else { print "matcht nicht\n"; } }
2012-11-09T16:46:55 GwenDragonWelche Information brauchst du denn genau aus dem Log?
1 2 3 4 5 6 7 8 9 10 11 12 13
$test[$z] =~ /^\[ ([^\]]+) # Datum \] \s+ \[ ([^\]]+) # Fehler \] \s+ (?>\[\S+\s([^\]]+)\]\s+)? # falls [client ...] Lookahead (.+) $ /x;
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
#!/usr/bin/perl use strict; use warnings; my @test; push @test,'[Thu Nov 08 22:59:19 2012] [error] Hostname www.......de provided via SNI, but no hostname provided in HTTP request'; push @test,'[Fri Nov 09 09:49:30 2012] [error] [client 8.1.1.1] Can\'t modify constant item in scalar assignment at /script.pl line 16, near "\'schiess mich tot\';", referer: https://.......pl'; for (my $z = 0; $z < scalar @test; $z++) { print "Zeile $z: "; if ( $test[$z] =~ /^\[ ([^\]]+) # Datum \] \s+ \[ ([^\]]+) # Fehler \] \s+ (?>\[\S+\s([^\]]+)\]\s+)? # falls [client ...] Lookahead (.+) $ /x; ) { print "matcht 1=>$1< 2=>$2< 3=>$3< 4=>$4< 5=>$5< 6=>$6< 7=>$7<\n"; } else { print "matcht nicht\n"; } }
Quotesyntax error at test.pl line 23, near "/x;"
(Might be a runaway multi-line // string starting on line 12)
syntax error at test.pl line 27, near "else"
Execution of test.pl aborted due to compilation errors.
2012-11-09T18:13:38 GwenDragonWenn du das in ein if packst ohne das ; am Ende!
2012-11-09T18:18:06 GwenDragonAch so, dann kannst du das Regex ja noch erweitern
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
#!/usr/bin/perl use strict; use warnings; my @test; push @test,'[Thu Nov 08 22:59:19 2012] [error] Hostname www.......de provided via SNI, but no hostname provided in HTTP request'; push @test,'[Fri Nov 09 09:49:30 2012] [error] [client 8.1.1.1] Can\'t modify constant item in scalar assignment at /script.pl line 16, near "\'schiess mich tot\';", referer: https://.......pl'; for (my $z = 0; $z < scalar @test; $z++) { print "Zeile $z: "; if ( $test[$z] =~ /^\[ [^ ]+ ([^ ]+) ?([0-9]+) ([0-9:]+) ([0-9]+) # Datum \] \s+ \[ ([^\]]+) # Fehler \] \s+ (?>\[\S+\s([^\]]+)\]\s+)? # falls [client ...] Lookahead (.+) $ /x ) { print "matcht 1='$1' 2='$2' 3='$3' 4='$4' 5='$5' 6='$6' 7='$7'\n"; } else { print "matcht nicht\n"; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/^\[ \S+\s+ (\S+)\s+ ([0-9]+)\s+([0-9:]+)\s+([0-9]+) # Datum \] \s+ \[ ([^\]]+) # Fehler \] \s+ (?>\[\S+\s([^\]]+)\]\s+)? # falls [client ...] Lookahead (.+) $ /x
2012-11-09T18:30:21 GwenDragonWenn ein Regex den Schalter x beinhaltet, ist ein Leerzeichen nur zur Verschönerung der Ansicht. Wenn du Leerzeichen matchen willst, mach das mit \s; Nichtleerzeichen matchen mit \S (das verhindert das grässliche [^ ]).
2012-11-09T18:39:52 GwenDragonNachtrag. Regexgruppen können in moderneren Perls auch benannt werden.
2012-11-09T18:54:55 GwenDragonFalls du dich beim Schreiben von komplexeren Rexen schwer tust, es gibt Tools zum probieren der Regexes. Ist keine Schande.
2012-11-09T19:04:23 GwenDragonqr ist dein Freund.
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
#!/usr/bin/perl use strict; use warnings; my $regex = qr{ /^\[ \S+\s+ (\S+)\s+ ([0-9]+)\s+([0-9:]+)\s+([0-9]+) # Datum \] \s+ \[ ([^\]]+) # Fehler \] \s+ (?>\[\S+\s([^\]]+)\]\s+)? # falls [client ...] Lookahead (.+) $ }x; my @test; push @test,'[Thu Nov 08 22:59:19 2012] [error] Hostname www.......de provided via SNI, but no hostname provided in HTTP request'; push @test,'[Fri Nov 09 09:49:30 2012] [error] [client 8.1.1.1] Can\'t modify constant item in scalar assignment at /script.pl line 16, near "\'schiess mich tot\';", referer: https://.......pl'; for (my $z = 0; $z < scalar @test; $z++) { print "Zeile $z: "; if ( $test[$z] =~ $regex ) { print "matcht 1='$1' 2='$2' 3='$3' 4='$4' 5='$5' 6='$6' 7='$7'\n"; } else { print "matcht nicht\n"; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
my $regex = qr{ ^\[ \S+\s+ (\S+)\s+ ([0-9]+)\s+([0-9:]+)\s+([0-9]+) # Datum \] \s+ \[ ([^\]]+) # Fehler \] \s+ (?>\[\S+\s([^\]]+)\]\s+)? # falls [client ...] Lookahead (.+) $ }sx;
QuoteUse of uninitialized value $7 in concatenation (.) or string at test.pl line 29.
Zeile 0: matcht 1='[Thu Nov 08 22:59:19 2012] [error] Hostname www.......de provided via SNI, but no hostname provided in HT
TP request' 2='Nov' 3='08' 4='22:59:19' 5='2012' 6='error' 7=''
Zeile 1: matcht 1='[Fri Nov 09 09:49:30 2012] [error] [client 8.1.1.1] Can't modify constant item in scalar assignment at /s
cript.pl line 16, near "'schiess mich tot';", referer: https://.......pl/' 2='Nov' 3='09' 4='09:49:30' 5='2012' 6='error' 7='
8.1.1.1'
Quote$ perl test.pl
Use of uninitialized value $6 in concatenation (.) or string at test.pl line 29.
Zeile 0: matcht 1='Nov' 2='08' 3='22:59:19' 4='2012' 5='error' 6='' 7='Hostname www.......de provided via SNI, but no hostname provided in HTTP request'
Zeile 1: matcht 1='Nov' 2='09' 3='09:49:30' 4='2012' 5='error' 6='8.1.1.1' 7='Can't modify constant item in scalar assignment at /script.pl line 16, near "'schiess mich tot';", referer: https://.......pl/'
2012-11-10T11:53:29 RaubtierIch bekomme mit dem obigen Code vor deinem Edit ohne den / das folgende Ergebnis:
1
2
3
Use of uninitialized value $7 in concatenation (.) or string at test.pl line 29.
Zeile 0: matcht 1='[Thu Nov 08 22:59:19 2012] [error] Hostname www.......de provided via SNI, but no hostname provided in HTTP request' 2='Nov' 3='08' 4='22:59:19' 5='2012' 6='error' 7=''
Zeile 1: matcht 1='[Fri Nov 09 09:49:30 2012] [error] [client 8.1.1.1] Can't modify constant item in scalar assignment at /script.pl line 16, near "'schiess mich tot';", referer: https://.......pl' 2='Nov' 3='09' 4='09:49:30' 5='2012' 6='error' 7='8.1.1.1'
1
2
3
4
Use of uninitialized value $6 in concatenation (.) or string at test.pl line 42.
Zeile 0: matcht 1='Nov' 2='08' 3='22:59:19' 4='2012' 5='error' 6='' 7='Hostname www.......de provided via SNI, but no hostname provided in HTTP request'
Zeile 1: matcht 1='Nov' 2='09' 3='09:49:30' 4='2012' 5='error' 6='8.1.1.1' 7='Can't modify constant item in scalar assignment at /script.pl line 16, near "'schiess mich tot';", ref
erer: https://.......pl'
2012-11-09T19:01:22 GwenDragonA propos, ich empfehle die Verwendung mehrzeiliger Regexe mit /x und Kommentierung mit # ...
Solche einzeiligen meterlangen Bandwürmer finde ich schwer nachvollziehbar und unlesbar. Aber vielleicht ist es auch das Alter. ;)