Solved

IP Counter

Posted on 2003-10-21
7
152 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

914 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now