CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Extract IP addresses and their occurence

 



alkesh
New User

Aug 27, 2013, 2:22 AM

Post #1 of 2 (1037 views)
Extract IP addresses and their occurence Can't Post

I have a log file which contains results for nslookup on URLs.

Example set of data in this log
URL
XX.XX.XX.XX
XX.XX.XX.XX
URL
XX.XX.XX.XX
XX.XXX.XX.XX

Now in this data log, IP addresses returned can be same or different.
Example.
www.google.com
173.194.72.104
173.194.72.105
173.194.72.106
173.194.72.147
173.194.72.99
173.194.72.103
(null)
www.cisco.com
60.28.176.170
60.28.176.170
60.28.176.170
60.28.176.170
60.28.176.170

Now in a new file I would like to print URL followed by IP address and occurence of each IP address.

What I have been able to do so far is below:


Code
use warnings; 
my %count;
my $file = 'DNS.log';

open (my $fh, '<', $file) or die "Could not open '$file': $!";

while (my $line = <$fh>)
{

if ($line =~ /(\d+.\d+.\d+.\d+)/){
my $ip = $1;
# print "$ip $count{$ip}\n";
#my $length = index ($line, " ");
#my $ip = substr($line, 0, $length);
$count{$ip}++;

}
}
foreach my $ip (keys %count) {
print "$ip $count{$ip}\n";
}


Above code gives me result as
60.28.176.170 5

But I would like to print URL before IP address and IP addresses are not in order as they appear in the data file.

Would appreciate your help. I guess I need to put a loop somewhere but I am not able to get that.


Chris Charley
User

Aug 27, 2013, 8:55 AM

Post #2 of 2 (1016 views)
Re: [alkesh] Extract IP addresses and their occurence [In reply to] Can't Post

I produced output below from my attached file (t9.pl).

Code
www.google.com 173.194.72.99 1 
www.google.com 173.194.72.103 1
www.google.com 173.194.72.104 1
www.google.com 173.194.72.105 1
www.google.com 173.194.72.106 1
www.google.com 173.194.72.147 1
www.cisco.com 60.28.176.170 5

Attachments: t9.pl (0.63 KB)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives