Analyze Log

2005-08-11 Thread Nick

Hi There,

I am trying to analyze a simple log file and pull 2 pieces of data from 
it. The log looks as follows:


www.example.com   42f3ca1042f8c42f0   73380 
  3638


Where each valie is tab seperated. I want to create a hash with 
www.example.com as the key and column 5 (7338 in this example) as the 
value. There will be lots and lots of lines hence me using foreach.


This is how I am attempting it:

#!/usr/bin/perl

use Data::Dumper;

@tmp = `cat /usr/local/apache/logs/tmp`;

foreach (@tmp) {
if 
(/(^.+(\.net|\.uk|\.com|\.org))(\t[a-z0-9]{9})(\t[a-z0-9]{9})(\t[a-z0-9]{9})(\t[a-z0-9]{9})/) 
{

$bw_usage{$1} = ( $6 );
}
}

print Dumper \%bw_usage;

Firstly, please don't laugh at my code too much! :o) Now I thought this 
would put coulmn 1 into memory 1 and column 5 intom memory 6. This does 
not seem to work at all and all I get is an empty hash!


Could anyone advise what I am doing wrong or if I am attempting this is 
the wrong way?


Thanks, Nick



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Analyze Log

2005-08-11 Thread mgoland


- Original Message -
From: Nick [EMAIL PROTECTED]
Date: Thursday, August 11, 2005 10:37 am
Subject: Analyze Log

 Hi There,
Hi Nick,
 
 I am trying to analyze a simple log file and pull 2 pieces of data 
 from 
 it. The log looks as follows:
 
 www.example.com   42f3ca1042f8c42f0   7338 
   0 
   3638
 
 Where each valie is tab seperated. I want to create a hash with 
 www.example.com as the key and column 5 (7338 in this example) 
 as the 
 value. There will be lots and lots of lines hence me using foreach.
Are you sure they are all tab separated ?? If this is a case, using C split 
would yield much more efficient code.

 
 This is how I am attempting it:
 
 #!/usr/bin/perl
always !!!
use strict;
use warnings;
 
 use Data::Dumper;
 
 @tmp = `cat /usr/local/apache/logs/tmp`;
Is there a reason for doing this externaly ? what happens if the cat fails ? 
are you sure your script is pulling in the data ??
 
 foreach (@tmp) {
 if 
 (/(^.+(\.net|\.uk|\.com|\.org))(\t[a-z0-9]{9})(\t[a-z0-9]{9})(\t[a-
 z0-9]{9})(\t[a-z0-9]{9})/) 
 {
 $bw_usage{$1} = ( $6 );
 }
what happens if the match fails ? Are you sure it matches on all itterations ?
  else{ print match failed \a\n!!;}

 }
 
 print Dumper \%bw_usage;
 
 Firstly, please don't laugh at my code too much! :o) Now I thought 
 this 
 would put coulmn 1 into memory 1 and column 5 intom memory 6. This 
 does 
 not seem to work at all and all I get is an empty hash!
 
 Could anyone advise what I am doing wrong or if I am attempting 
 this is 
 the wrong way?
I think my pointers above would tell you why it's failing. Here is an aproach I 
would take...

#!PERL
use warnings;
use strict;

my ($debug,%hash);
open RD,/usr/local/apache/logs/tmp or die ERROR ON OPENING LOG!!\n;

while( RD ){
 my @tmp = split \t;
$hash{$tmp[0]} = $tmp[4];
print join :,@tmp if $debug;
}

HTH,
Mark G.
 
 Thanks, Nick
 
 
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response
 
 
 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Analyze Log

2005-08-11 Thread Matija Papec

Nick wrote:


Hi There,

I am trying to analyze a simple log file and pull 2 pieces of data from 
it. The log looks as follows:


www.example.com   42f3ca1042f8c42f0   73380 
  3638


Where each valie is tab seperated. I want to create a hash with 
www.example.com as the key and column 5 (7338 in this example) as the 
value. There will be lots and lots of lines hence me using foreach.


This is how I am attempting it:

#!/usr/bin/perl

use Data::Dumper;

@tmp = `cat /usr/local/apache/logs/tmp`;

foreach (@tmp) {
if 
(/(^.+(\.net|\.uk|\.com|\.org))(\t[a-z0-9]{9})(\t[a-z0-9]{9})(\t[a-z0-9]{9})(\t[a-z0-9]{9})/) 
{

$bw_usage{$1} = ( $6 );
}
}

print Dumper \%bw_usage;

Firstly, please don't laugh at my code too much! :o) Now I thought this 


I think, keep it simple is a key rule most of the time. :)
If you have tab separated values, then spliting on \t should be very 
straightforward,


#untested
my %bw_usage;
for my $line (@tmp) {
  my ($k, $v) = (split /\t/, $line)[0,4];
  #domain test?
  next if $k !~ /\.(net|uk|com|org)$/i;

  $bw_usage{$k} = $v;
}
print Dumper \%bw_usage;


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Analyze Log

2005-08-11 Thread Binish A R

Nick wrote:


Hi There,

I am trying to analyze a simple log file and pull 2 pieces of data 
from it. The log looks as follows:


www.example.com   42f3ca1042f8c42f0   7338
0   3638


Where each valie is tab seperated. I want to create a hash with 
www.example.com as the key and column 5 (7338 in this example) as 
the value. There will be lots and lots of lines hence me using foreach.


This is how I am attempting it:

#!/usr/bin/perl

use Data::Dumper;

@tmp = `cat /usr/local/apache/logs/tmp`;

foreach (@tmp) {
if 
(/(^.+(\.net|\.uk|\.com|\.org))(\t[a-z0-9]{9})(\t[a-z0-9]{9})(\t[a-z0-9]{9})(\t[a-z0-9]{9})/) 
{

$bw_usage{$1} = ( $6 );
}
}

print Dumper \%bw_usage;

Firstly, please don't laugh at my code too much! :o) Now I thought 
this would put coulmn 1 into memory 1 and column 5 intom memory 6. 
This does not seem to work at all and all I get is an empty hash!


Could anyone advise what I am doing wrong or if I am attempting this 
is the wrong way?


Thanks, Nick




How about the following code ...

#!/usr/bin/perl -w

open FD, /usr/local/apache/logs/tmp;

while (FD) {
   $bw_usage{$1} = $2 if /([\w.]+)\s+\w+\s+\w+\s+\w+\s+(\w+).+/;
}

close FD;

foreach (keys %bw_usage) {
   print $_, $bw_usage{$_}\n;
}




--
Get Thunderbird http://www.mozilla.org/products/thunderbird/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response