my %hash while(<>){ my $key = join " ", (sort (split " ")); $hash{$key} = 1; } #now iterate over the keys of the hash, and either print them out, or do your insert in to the database