Thread Richtig IPs parsen (4 answers)
Opened by Alexander at 2012-04-19 21:30

topeg
 2012-04-19 22:17
#157679 #157679
User since
2006-07-10
2611 articles
BenutzerIn

user image
Hier wie man es machen kann:
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
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $file='logs.txt';

my @ips;
my @agents;
my @timeresult;

open(my $fh, '<', $file) or die("error open $file ($!)\n");
while(my $line=<$fh>)
{
  if($line=~/([\d.]+) - - \[(.+?)\] "(.+?)" (\d+) (\d+) "(.+?)" "(.+?)"/)
  {
    my ($ip,$time,$request,$result,$filesize,$referer,$useragent)=($1,$2,$3,$4,$5,$6,$7);
    push(@timeresult,$time);
    push(@ips,$ip);
    push(@agents,$useragent);
  }
}
close($fh);

my $starttime=$timeresult[0];
my $endtime=$timeresult[-1];

print Dumper(\@ips);
print Dumper(\@agents);


Du kannst das ganze aber auch in einer Datenstruktur speichern.
Hier werden die daten in einem Array Of Hashes (AoH) gespeichert:
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
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $file='logs.txt';

my @data;

open(my $fh, '<', $file) or die("error open $file ($!)\n");
while(my $line=<$fh>)
{
  if($line=~/([\d.]+) - - \[(.+?)\] "(.+?)" (\d+) (\d+) "(.+?)" "(.+?)"/)
  {
    push(@data,{
        ip       => $1,
        time     => $2,
        request  => $3,
        result   => $4,
        filesize => $5,
        referer  => $6,
        useragent=> $7,
      });
  }
}
close($fh);

my $starttime=$data[0]->{time};
my $endtime=$data[-1]->{time};

print Dumper(\@data);

View full thread Richtig IPs parsen