use strict; my %total; while () { next if /^#/; next if /^$/; next if ! /(server[1-4])\s+(proc[A-Z]+)\s+(proc\.[0-9]+)\s+[0-9]+\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:?[0-9]{4}?)\s+.+$/; $total{join ("\@", $3,$1)} = "$4"; } my @matches = (); while (<>) { chomp; my $item = $_; #@matches = grep {$_ eq $item} (keys %total); for (keys %total) { push @matches, $_ if $_ eq "$item"; } } for (@matches) { print "$_ ===> $total{$_}\n"; }