Solved

IP Counter

Posted on 2003-10-21
7
151 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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.

747 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

12 Experts available now in Live!

Get 1:1 Help Now