EDIT: It somehow escaped me that others had suggested effectively this very thing, although I did use a little used operator for printing out the DUP records, so technically it's a different solution! I so need to go home.
Is there some reason why just reading all the keys into a hash while incrementing the value wouldn't net you what you want?
my %hash;
while ( <IN> ) {
$hash{ $_ }++;
}
foreach my $key ( sort keys %hash ) {
if ( $hash{ $key } > 1 ) {
print DUP "$key\n" x $hash{ $key };
} else {
print UNQ "$key\n";
}
}