This is the code I wrote. I am trying to output the results to a file. I used the open()
but it does not work. Can someone please tell me what to do?
AP. result is the file in which I would like to copy the result to. However it does not copy. The results are still showing in the screen.
open (Host, >, AP.results) or die;
for my $key (@keyA){
printf "%-5i %-6.2f %-12s\n", $hash{$key}, 100*($hash{$key}/$logcount), $key;
}
print "----\n$logcount entries displayed\n\n";
}
my $resCount =0;
my $logcount = 0 ;
my $bs = 0;
for my $i (@ARGV){
open IN, $i;
my $regex= '(.*?)\s-.*?-\s\[(.*?):(.*?)\s-.*?\]\s"(.*?)\s(.*?)\s(.*?)\s(.*?)\s(.*?)\s"(.*?)"\s"(.*)';
while (my $data = <IN>){
if ($data =~ $regex){
$logcount++;
my $paddr = $1;
my $pdate = $2;
my $pstatus = $7;
my $pURL = $5;
my $pTime = $3;
$bs +=$8;
my $pUserAgent = $10;
if ($9 eq '-'){
$referer{'NO REFERER'}++;
$rdomain{'NO REFERER'}++;
}else{
$referer{$9}++;
if($9 =~ '.*\/\/(?:www\.){0,1}([^\/\s]*)'){
$rdomain{$1}++;
}
}
my $domain = $paddr;
$ip{$paddr}++;
$date{$pdate}++;
$status{$pstatus}++;
$URL{$pURL}++;
$userAgent{$pUserAgent}++;
if ( $pTime =~ '(\d+):.*' ){
$hour{$1}++;
}
if ($pURL =~ '.*(\.[^\/\s]*).*'){
$resource{$1}++;
$resCount++;
}
if ($pUserAgent =~'.*Microsoft.*'){
$OS{Windows}++;
}elsif($pUserAgent =~'.*Mac OS X.*'){
$OS{Macintosh}++;
}elsif($pUserAgent =~'.*Linux.*'){
$OS{Linux}++;
}else{
$OS{Other}++;
}
if ($pUserAgent =~'.*Mozilla.*'){
$aFamily{Firefox}++;
}elsif ($pUserAgent =~'.*Chrome.*'){
$aFamily{Chrome}++;
}elsif ($pUserAgent =~'.*(Internet Explorer|MSIE).*'){
$aFamily{'Internet Explorer'}++;
}elsif ($pUserAgent =~'.*Opera.*'){
$aFamily{Opera}++;
}elsif ($pUserAgent =~'.*Safari.*'){
$aFamily{Safari}++;
}else{
$aFamily{Unknown}++;
}
#print "$pURL\n";
}
}
close IN;
}
my @fdate = sort(keys %date);
my @fip = sort(keys %ip);
my @fhour = sort(keys %hour);
my @fstatus = sort(keys %status);
my @fURL = sort(keys %URL);
my @fResource = sort(keys %resource);
my @fUserAgent = sort(keys %userAgent);
my @faFamily = sort(keys %aFamily);
my @freferer = sort(keys %referer);
my @frdomain = sort(keys %rdomain);
my @fOS = sort(keys %OS);
printFormatter(\%ip, \@fip, $logcount, "IPADDR");
printFormatter(\%date, \@fdate, $logcount, "DATE");
printFormatter(\%hour, \@fhour, $logcount, "HOUR");
printFormatter(\%status, \@fstatus, $logcount, "STATUS");
printFormatter(\%URL, \@fURL, $logcount, "URL");
printFormatter(\%resource, \@fResource, $resCount, "RESOURCE");
printFormatter(\%userAgent, \@fUserAgent, $logcount, "USER AGENT");
printFormatter(\%aFamily, \@faFamily, $logcount, "BROWSER FAMILY");
printFormatter(\%referer, \@freferer, $logcount, "REFERER");
printFormatter(\%rdomain, \@frdomain, $logcount, "REFERER DOMAIN");
printFormatter(\%OS, \@fOS, $logcount, "OPPERATING SYSTEM");
print "\n\n$bs bytes have been served!\n";