Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Dec 22nd, 2007, 9:48 AM   #12
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Count number of occourences of a IP address in a log file.

Hah, I can't believe Perl can actually do that in a matter of minutes. But it's still pretty clear that the associative array takes the cake with half a second.

Thanks for that demo. Cheers.
Sane is offline   Reply With Quote
Old Dec 22nd, 2007, 10:42 AM   #13
lynxus
Newbie
 
Join Date: Dec 2007
Posts: 4
Rep Power: 0 lynxus is on a distinguished road
Re: Count number of occourences of a IP address in a log file.

Hey guy's,

Glad your all debating how to do this..

I have NO idea what that code does so could anyone clarify what i need to do to achieve my goal hehe.

Thanks again.
Graham
lynxus is offline   Reply With Quote
Old Dec 22nd, 2007, 11:26 AM   #14
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Count number of occourences of a IP address in a log file.

mbd's posted code snippet outputs how many instances, of each IP address, are in the log file that Jessehk posted. I'd recommend using his second snippet (which is the psuedocode that I suggested in my first post), since it takes less than half a second to execute. If you know Perl you should be able to modify it to fit your needs.
Sane is offline   Reply With Quote
Old Sep 24th, 2008, 8:19 AM   #15
vikas1234
Newbie
 
Join Date: Aug 2008
Posts: 5
Rep Power: 0 vikas1234 is on a distinguished road
Re: Count number of occourences of a IP address in a log file.

Nice code in C++ .... But perl regex is best to handle this type of problem
vikas1234 is offline   Reply With Quote
Old Sep 24th, 2008, 11:36 AM   #16
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 257
Rep Power: 2 Fall Back Son is on a distinguished road
Re: Count number of occourences of a IP address in a log file.

^ Why? If he just supplied c++ code that finds a solution in half a second, I would say that the language is perfectly good to handle the problem.
Fall Back Son is offline   Reply With Quote
Old Sep 24th, 2008, 2:56 PM   #17
KevinADC
Newbie
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0 KevinADC is on a distinguished road
Re: Count number of occourences of a IP address in a log file.

Quote:
Originally Posted by Sane View Post
An O(N^2) algorithm? What if the file is 100kb?

Even if it compared 10,240 bytes per second. It would still take 12 days to run it only once.

print (100*1024)**2/10240.0/3600.0/24.0

That's one of the worst implementations for such a simple problem I've ever come across.


An O(N) algorithm, such as using an associative array, is just as simple to implement, and will take 10 seconds under the exact same circumstances...

print (100*1024)/10240.0
Did your entire foot fit into your mouth?
KevinADC is offline   Reply With Quote
Old Sep 24th, 2008, 3:04 PM   #18
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 270
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: Count number of occourences of a IP address in a log file.

ZIP, im gonna think infuture
__________________
Steven Skiena - Algorithms

Last edited by Freaky Chris; Sep 24th, 2008 at 3:15 PM.
Freaky Chris is online now   Reply With Quote
Old Sep 24th, 2008, 6:57 PM   #19
KevinADC
Newbie
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0 KevinADC is on a distinguished road
Re: Count number of occourences of a IP address in a log file.

if all that is in the file is an ip addresses per line there is no need to use a regexp:

#!/usr/bin/perl

@log = <>;
 ++$count{$_} for @log;
foreach $ip (keys %count) {
   print "$ip $count{$ip}\n" if ;	
}

that should run very fast.
KevinADC is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Jumping to a specific line number in a text file Adeil C++ 4 Nov 5th, 2007 9:54 AM
PigLattin Converter, count number of words used in dictionary. MrSmiley Python 2 Oct 17th, 2005 4:47 PM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM
Specific file address... Monkey_features Delphi 0 Feb 8th, 2005 5:05 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC