View Single Post
Old Dec 21st, 2007, 7:03 PM   #11
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Count number of occourences of a IP address in a log file.

perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2.  
  3. @log = <STDIN>;
  4. $log = join '', @log;
  5. while ($log =~ m/(\d+\.\d+\.\d+\.\d+\s)/g)
  6. {
  7. $ip = $1;
  8. chomp $ip;
  9. $count = ($log =~ s/$1/$1/g);
  10. print "$ip $count\n";
  11. }
$ time cat ipaddr.txt | perl count.pl > report.txt

real	4m9.138s
user	3m7.281s
sys	0m58.301s
if i were going to run this more than once, i would probably change it to your method like this
perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2.  
  3. @log = <STDIN>;
  4. $log = join '', @log;
  5. while ($log =~ m/(\d+\.\d+\.\d+\.\d+\s)/g)
  6. {
  7. $ip = $1;
  8. chomp $ip;
  9. ++ $count{$ip};
  10. }
  11. foreach $ip (keys %count)
  12. {
  13. print "$ip $count{$ip}\n";
  14. }
$ time cat ipaddr.txt | perl hashcount.pl > report.txt

real	0m0.501s
user	0m0.433s
sys	0m0.052s
happy holidays all
Attached Files
File Type: txt report.txt (1.55 MB, 11 views)
mbd is offline   Reply With Quote