Home > Archive > PERL Beginners > March 2006 > Hash -> Complete dummie question
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 |
Hash -> Complete dummie question
|
|
| LuxMan2006@gmail.com 2006-03-21, 7:55 am |
| Dear all,
I am extremly new to Perl and it's wonders. Now i have cause to try my
skills and i found my self very lost. I have read so many howto's and
group postings but have so far not been able to find out how to fix my
small script.
I have a logfile from an apache server. I have extracted all pages
that i am interested in and built my self a little hash as shown
below. My question is, how do i use this list to take out statistics
from an external file on how many times each one of these pages has
been accessed? Is there another way to do this?
What little i have so far,
%MyJsp = (
'AccountList.jsp' => 0
'AssetsCurrencyBreakdown.jsp' => 0
'AssetsDrillDown.jsp' => 0
etc
etc
);
Regards and thanks a lot for any help,
Pierre
| |
| usenet@DavidFilmer.com 2006-03-21, 9:58 pm |
| LuxMan2006@gmail.com wrote:
> I have a logfile from an apache server. I have extracted all pages
> that i am interested in and built my self a little hash as shown
You do not need to "pre-build" or initialize a hash. You do not need to
extract anything from the logfile - your script can operate directly on
the logfile itself (building the hash key/value pairs for EVERYTHING
"on the fly") and then show you only what you want to see. It's not
the most efficient approach, but it's easier (for the programmer). For
example:
#!/usr/bin/perl
use strict; use warnings;
my %page_count;
open my $log, '/var/log/apache2/access_log' || die $!;
while (<$log> ) {
/^.*?GET \S*\/(\S+)/ || next; #get the name of the page
$page_count{$1}++;
}
# Now print the page count for those pages which interest me.
foreach my $page( qw{ index.html robots.txt bogus.html } ) {
printf "%s has been accessed %s times\n",
$page,
$page_count{$page} || 0;
}
__END__
--
http://DavidFilmer.com
| |
| LuxMan2006@gmail.com 2006-03-23, 3:58 am |
| Hello David,
Thank you so much for your help. It worked like a charm. My only
misstake was that i did not tell you everything. Our apache log is a
little 'tuned' so the output differs from the normal apache Logs. All i
had to do was to figure out how to change your Reg. Expressions. So
after 1h it was all working a little better.
Now my next problem to solve is how to open up 50 compressed files
(archive dir) and do the same ;-)
Regards,
Pierre
Adding your 'tuned' script.
#!/usr/bin/perl
use strict; use warnings;
my %page_count;
open my $log, '/var/log/cindy/pts3.log' || die $!;
while (<$log> ) {
#/^.*?GET \S*\/(\S+)/ || next; #get the name of the page
/^.*?Request for \/.*?\/(\w+\.\w+):/ || next; #get the name of
the page
$page_count{$1}++;
}
# Now print the page count for those pages which interest me.
foreach my $page( qw{
AccountList.jsp
AssetsCurrencyBreakdown.jsp
AssetsDrillDown.jsp
AssetsFinancialInstruments.jsp
etc
etc
etc
LogOff.jsp } ) {
printf "%s \t\,thas been accessed,\t\t %s times\n",
$page,
$page_count{$page} || 0;
}
| |
| usenet@DavidFilmer.com 2006-03-23, 7:58 am |
| LuxMan2006@gmail.com wrote:
> Now my next problem to solve is how to open up 50 compressed files
> (archive dir) and do the same ;-)
That's not a problem, that's a piece-o-cake! This is Perl you're
coding with, and you have the power of CPAN at your side.
You can
use PerlIO::gzip; #a module from CPAN
and then make just one minor change to the open() command:
open my $log, "<:gzip", "name.of.my.file.gz" or die $!;
The rest of the code is unaffected. The gzipped logfile will be
decompressed on the fly; the rest of the script sees it as a plain,
ordinary file. Obviously you wrap that code block in a loop that
processes each file (presumably from a readdir() function), using a
variable in place of the hard-coded filename in my example statement
above.
You could accomplish the same thing with Tie::Gzip module.
Welcome to the wonder that is Perl.
(if you wish to follow up this topic, however, you should do so in a
new thread, since this is getting off-topic from your original post)
--
http://DavidFilmer.com
| |
| LuxMan2006@gmail.com 2006-03-27, 3:57 am |
| Hello again and once again thanks a lot for all this excellent
information.
My life has become rather simple now after your help. Its still fuzzy
but becoming slightly less fuzzy. ;-)
Agrred i will start a new thread regaring zcat and opening file by
file....
Thanks a bunch!
Regards,
Pierre
|
|
|
|
|