my ($cap, %dest); # Open a file and slurp in the entire contents to one scalar open(CAP, "$file"); while() { $cap .= $_; } close CAP; # Scan through the contents for source/dest pairs and keep track of how many times the dest gets hit: while($cap =~ /\s+(?:(?:\d{1,3}\.){3}\d{1,3})\s+->(\d{1,3}\.){3}\d{1,3})\s+/g) { unless(exists($dest{$1})) { $dest{$1} = 1; } else { $dest{$1}++; } } # Print out the destinations and the number of times they were hit in descending order foreach my $dest_addr (sort { $dest{$b} <=> $dest{$a} } keys %dest) { print "$dest_addr\t$dest{$dest_addr}\n"; }