my ($line, %hash); while ($line = ) { if ($line =~ /(your_sequence_format)/) { if (! exists($hash{$1})) { $hash{$1} = $line; } } } # now you can print the hash back to your file