Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

IP Counter

Posted on 2003-10-21
7
Medium Priority
?
160 Views
Last Modified: 2010-03-04
This is not a homework assignment.
I need to record any IPs that appear more thn 50 times in a file.  Once the IP appears more than 50 times in a file, I need to copy that IP address to a new file.

How is this done in perl?
0
Comment
Question by:uluttrell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9592273

You can just create a hash, with the key as an IP. Each time that you encounter an IP, increase the value of that IP in the hash by 1.  Something like this should work - it's not tested, but it should at least give you an idea.

my %ips;


while (<>) {
     #code to read in IP
     $ips{$currentip}++;
}


foreach ( keys($ips) ) {
     print if ($ips{$_} > 0);

0
 
LVL 8

Expert Comment

by:inq123
ID: 9592510
well, to make the code more complete (and confusing :-) ):

while(<>)
{
  print "$1\n" if(/((?:\d+\.){3}\d+)/ && ++$ips{$1} > 50);
}

The above will give you ip address reading, counting, and printing.
0
 
LVL 8

Accepted Solution

by:
inq123 earned 500 total points
ID: 9592527
if you expect more than one IP per row, you should use this code:

while(<>)
{
  while(/((?:\d+\.){3}\d+)/g)
  {
    print "$1\n" if(++$ips{$1} > 50);
  }
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 28

Expert Comment

by:FishMonger
ID: 9595129
while (<>) {
   ++$ip{$1} if (/((\d{1,3}\.){3}\d{1,3})/g);
}

open OUT, ">redundant_ip" or die "could not open reduntant_ip file <$!>";
foreach $ip (sort keys %ip) {
   print $ip if ($ip{$ip} > 50);
}
close OUT;
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9595146
I forgot to include the FH and the \n on the print statement, so it should read:
   print OUT "$ip\n" if ($ip{$ip} > 50);
0
 

Author Comment

by:uluttrell
ID: 9607288
inq123,
Your code is best suited for what I need.  I am awarding the points to you.
0
 
LVL 8

Expert Comment

by:inq123
ID: 9607300
Thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question