Hi,
I am very new to perl pgming. I have a weblog which has a standard format as the below example


IP | remoteUser | authUser | Date/Time | Request | Status | Bytes
For example,
74.6.72.229 - - [27/Jan/2007:00:00:36 -0500] "GET /~jking/ralph.html HTTP/1.0" 404 215

the fields are seperated by space.
I need to find what are the missing pages in the website. I tried writing the below code.


I am not able to match the index of the status array with the uri array. could some one please tel me how to do this.

foreach $logfile ("sample.log")
	{
	open (INF, $logfile);
	read INF, $file, -s INF;
	close INF;
	@lines = split /\n/, $file;
	foreach (@lines) {
	@values = split / /, $_;
	
	$status{$values[8]}++;
	

	$uri{$values[7]}++;

		print "\n\n\n Status codes returned \n";
	foreach (keys %status)
		{
		
		print "\n The status code " . $_ . " was returned ". $status{$_} . " times";
	if($_ == 404)
		{	print "\nMissing page or misspelt uri " . $uri($_) . "bad";}
			
		}

Edited 6 Years Ago by abdulraqeeb33: gave example

I changed the code to ..but aint getting any output...

foreach $logfile ("sample.log")
{
open (INF, $logfile);
read INF, $file, -s INF;
close INF;

@lines = split /\n/, $file;

foreach (@lines){
@values = split / /, $_;
$status{$values[8]}++;
$uri{$values[7]}++;
}


foreach (keys %status)
{
print "\n The status code " . $_ . " was returned ". $status{$_} . " times";

if($_ == 404)
{print "\nMissing page or misspelt uri".$uri{$_}." bad";}

}
}

Hi,
I am very new to perl pgming. I have a weblog which has a standard format as the below example


IP | remoteUser | authUser | Date/Time | Request | Status | Bytes
For example,
74.6.72.229 - - [27/Jan/2007:00:00:36 -0500] "GET /~jking/ralph.html HTTP/1.0" 404 215

the fields are seperated by space.
I need to find what are the missing pages in the website. I tried writing the below code.


I am not able to match the index of the status array with the uri array. could some one please tel me how to do this.

foreach $logfile ("sample.log")
	{
	open (INF, $logfile);
	read INF, $file, -s INF;
	close INF;
	@lines = split /\n/, $file;
	foreach (@lines) {
	@values = split / /, $_;
	
	$status{$values[8]}++;
	

	$uri{$values[7]}++;

		print "\n\n\n Status codes returned \n";
	foreach (keys %status)
		{
		
		print "\n The status code " . $_ . " was returned ". $status{$_} . " times";
	if($_ == 404)
		{	print "\nMissing page or misspelt uri " . $uri($_) . "bad";}
			
		}

For your example i derive the below table..

Index -> Field -> Variable
---------------------------------
0 -> IP -> 74.6.72.229
1 -> remoteUser -> NULL
2 -> authUser -> NULL
3 -> Date/Time -> [27/Jan/2007:00:00:36 -0500]
4 -> Request -> GET /~jking/ralph.html HTTP/1.0
5 -> Status -> 404
6 -> Bytes -> 215

I assumed status code are repeated and uri are uniq things.

For the above table uri and status positions are 4 and 5 respectively. But you are mentioned in the program 7 and 8. Based on your example i modified the below code. you can try this code.

foreach $logfile ("sample.log")
{
   open (INF, $logfile);
   read INF, $file, -s INF;
   close INF;

   @lines = split /\n/, $file;

   foreach (@lines)
   {
      @values = split / /, $_;
      # Increment the status value
      $status{$values[5]}++;
    
      #$uri{GET /~jking/ralph.html HTTP/1.0}=404;
      # Assign the staus value for the corresponding uri
      $uri{$values[4]}=$values[5]; 
   }
}

foreach (keys %status)
{
 print "\n The status code " . $_ . " was returned ". $status{$_} . " times";
 if($_ == 404)
 {
  foreach my $key (keys %uri)
  {
   ## print the uri value equals to 404
   print "\nMissing page or misspelt uri ".$key." bad" if ( $uri{$key} == 404) ;
   }
 }
}

Edited 6 Years Ago by k_manimuthu: n/a

Got the solution..here is the code

foreach $logfile("sample.log")
{
$i=0;
open (INF, $logfile);
read INF, $file, -s INF;
close INF;

@lines = split /\n/, $file;

foreach (@lines){
@values = split / /, $_;
if($values[8]==404){$missing{$values[6]}++;}

}

foreach (keys %missing){print "\n The missing pages are " . $_ . " ";$i++;}
print "\nTotal number of missing links are ".$i;
}

I changed the code to ..but aint getting any output...

foreach $logfile ("sample.log")
{
open (INF, $logfile);
read INF, $file, -s INF;
close INF;

@lines = split /\n/, $file;

foreach (@lines){
@values = split / /, $_;
$status{$values[8]}++;
$uri{$values[7]}++;
}


foreach (keys %status)
{
print "\n The status code " . $_ . " was returned ". $status{$_} . " times";

if($_ == 404)
{print "\nMissing page or misspelt uri".$uri{$_}." bad";}

}
}
This question has already been answered. Start a new discussion instead.