View Single Post
Old Dec 21st, 2007, 10:10 AM   #4
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 is too powerful to loop through a log file counting up instances of different ip addresses. just use something like this.

perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2.  
  3. $log = "1.1.1.1 2.2.2.2 3.3.3.3 2.2.2.2 1.1.1.1 1.1.1.1";
  4. while ($log =~ m/(\d\.\d\.\d\.\d)/g)
  5. {
  6. $ip = $1;
  7. $count = ($log =~ s/$1/$1/g);
  8. print "$ip $count\n";
  9. }
mbd is offline   Reply With Quote