-----Original Message-----
From: John W. Krahn [mailto:[EMAIL PROTECTED] 
Sent: Thursday, September 13, 2007 12:42 PM
To: Perl beginners
Subject: Re: adding data to a file before it gets regexed

Moon, John wrote:
> 
> thanks all for the replies, I'm trying to parse log files.
> 
> so I got a bunch of logs in a directory, put them together and then
> parse them, I'm trying to keep a reference of the log files, so I
> know what log they came from, as I'm need to keep the file name to
> put it in to a another script.
> 
> I think from what people are suggesting I will have to use hashes, is
> there any good example log file perl scripts out there? or a good site
?
> 
> as I'm trying to take the logs that I find interesting and then
display
> them on a web page using html templates
> 
> Thanks for all the help
> Pat
> 
> [>>] 
> Here's a "way" ... Hope this gives you some ideas...
> 
> SUN83-PRODWEB>more ~/foo
> #! /usr/local/bin/perl
> use strict;
> 
> opendir HERE, '.';

You should *always* verify that the directory opened correctly:

opendir HERE, '.' or die "Cannot open '.' $!";


> my %logs;
> my $logs_total;
> while (my $filenm = readdir HERE) {
>         $logs_total++ if $filenm =~ 'log$';
>         next unless $filenm =~ '^access_sun83.*log$';
>         open LOG, $filenm;

You should *always* verify that the file opened correctly:

         open LOG, '<', $filenm or die "Cannot open '$filenm' $!";


>         my $cnt;
>         while (my $logdata = <LOG>) {
>                 $cnt++;
>                 }
>         close LOG;
>         $logs{$filenm} = $cnt;
>         }
> close HERE;
> printf "There are %d logs HERE\n", $logs_total;
> printf "I processed %d logs\n", scalar(keys %logs);
> foreach my $filenm (sort keys %logs) {
>         print "There are $logs{$filenm} items in $filenm\n";
>         }

Here is another way to do it:

my $logs_total = () = <*log>;
my $processed = @ARGV = <access_sun83*log>;
my %logs;
$logs{ $ARGV }++ while <>;

print "There are $logs_total logs HERE\n";
print "I processed $processed logs\n";
for my $filenm ( sort keys %logs ) {
     print "There are $logs{$filenm} items in $filenm\n";
     }




John
[>>] 
Look like it gave someone some ideas... ;-) 

Since we're "going there" you _should_ also check on *close* (on an
output file) - may save you a lot of time...

As for slurping in a "log" file ... some can be *very* large... 


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


Reply via email to