#! perl -sw use strict; open FH, '<', $ARGV[ 0 ] or die $!; ## First pass; find all the values my %vals; m[(\S+)$] and ++$vals{ $1 } while ; ## Select the top 10 and put them in a hash for fast lookup. my %top10; undef @top10{ ( sort{ $b <=> $a } keys %vals )[ 0 .. 9 ] }; ## rewind for second pass seek FH, 0, 0; ## rewind ## And print them if they fit the criteria m[(\S+)$] and exists $top10{ $1 } and print while ; ## close close FH;