Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

count the number of IP shows up in the file

by sgowrish (Initiate)
on Jan 20, 2009 at 21:44 UTC ( #737693=perlquestion: print w/ replies, xml ) Need Help??
sgowrish has asked for the wisdom of the Perl Monks concerning the following question:

Hi Please can someone guide me how to do this ? 1. I have a data file "path.csv" and it's contents is: 2. here basically i need to count the number of times each IP address shows up in the file based on the KEY 3. After loading above KEY:VALUES pair to HASH, i want to read IP and count based on each KEY and print into a file. I have coded something like below,i had given only the function:
sub _Load_IP { if(! open (PATH_FILE_FH, "$Path_File")) { print "Failed to Open input PATH_REPORT File: $Path_File\n"; exit 1; } while(<PATH_FILE_FH>) { my $data =$_; chomp($data); my($pname,@val) = split(/\s+/,$data); my($Starpnt,$Endpnt) = split(/_/,$pname); $key = $Startpnt; #$IP_hash{$key} .= exists $IP_hash{$key} ? map{$IPcountList{$_}++ }" +@val" : @val; $IP_hash{$key} .= "@val"; } close(PATH_FILE_FH); } #End of _Load_IP()

Comment on count the number of IP shows up in the file
Download Code
Re: Tally up the number of times each IP shows up in the file and load into HASH
by borisz (Canon) on Jan 20, 2009 at 21:57 UTC
    Not sure if I understand what you want to do. But here is my try.  script.pl < path.csv
    use strict; use warnings; my %h; while (<>) { my ( $k, @ips ) = split /\s+/; $k =~ s/(_.*)$//; $h{$k}{$_}++ for @ips; } for my $srv ( sort keys %h ) { for ( sort keys %{ $h{$srv} } ) { printf( "%10s %15s %6s\n", $srv, $_, $h{$srv}{$_} ); } }
    __OUTPUT__ 11BT801 10.197.28.38 1 11BT801 10.197.5.82 2 11BT801 10.200.10.10 1 11BT801 10.200.10.11 1 11BT801 10.200.6.42 2 11BT801 10.200.6.50 1 11BT801 10.200.8.26 2 12GT601 10.100.7.14 1 12GT601 10.100.9.16 2 12GT601 10.160.9.16 1 12GT601 10.180.7.13 1 12GT601 10.197.27.36 1 12GT601 10.197.5.82 2 12GT601 10.200.10.10 1 12GT601 10.200.5.40 2
    Boris
      Thank you so much.....

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://737693]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-10-24 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls