For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > Spliting the log files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Spliting the log files
Sivasakthi

2007-08-30, 3:59 am

Hi All,

I have used the following code to calculate from log files,

#!/usr/bin/perl
use strict;
use warnings;
use File::Tail;
my $file=File::Tail->new("/log/path");
my $line= undef;
while (defined($line=$file->read))
{
my ($time,$lport,$ip,$stats,$rport) = split;
print "$ip/n";
}

The log files is updated .. but it doesn't printed ..

The sample log file lines are,
1176369096.111 468 172.16.2.80 TCP_MISS/200 9629
1176378643.614 458 172.16.2.80 TCP_MISS/200 9626
1176378681.984 662 172.16.2.75 TCP_MISS/200 9626

Could u help me to find soln???

Thanks,
Siva

John W. Krahn

2007-08-30, 3:59 am

sivasakthi wrote:
> Hi All,


Hello,

> I have used the following code to calculate from log files,
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use File::Tail;
> my $file=File::Tail->new("/log/path");
> my $line= undef;


You don't really need file scope for $line.

> while (defined($line=$file->read))


You can limit its scope here:

while ( defined( my $line = $file->read ) )

> {
> my ($time,$lport,$ip,$stats,$rport) = split;


The current line is in the $line variable but you are splitting the contents
of the $_ variable:

my ( $time, $lport, $ip, $stats, $rport ) = split ' ', $line;

If you only need the third field then you could write that as:

my $ip = ( split ' ', $line )[ 2 ];

> print "$ip/n";


If you want a newline at the end it is spelt "\n", not "/n".

> }




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com