Solved

IP Counter

Posted on 2003-10-21
7
158 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 125 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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

636 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