#!/usr/bin/perl use strict; my (%hash, %hash_key); # hashes my $x = 0; # counters while (){ #starts reading the data line by line my @array = split(' ', $_); # split the data using spaces $x++; # global counter $hash{$array[1]}->{$array[4]} = $_; # primary database $hash_key{$x}= [$array[1],$array[4]]; # key database } foreach my $i (sort {$a <=> $b} keys %hash_key){ (exists $hash{$hash_key{$i}->[0]}->{$hash_key{$i}->[1]}) ? (print "$hash{$hash_key{$i}->[0]}->{$hash_key{$i}->[1]}") : (print "deleted\n"); # if the record in the database (hash) exists print it out otherwise print 'deleted' next if ($hash_key{$i}->[0] eq ''); # you need the empty lines so if you reached an empty line, skip the deleting part delete($hash{$hash_key{$i}->[0]}->{$hash_key{$i}->[1]}) if (exists $hash{$hash_key{$i}->[0]}->{$hash_key{$i}->[1]} || $hash{$hash_key{$i}->[1]}->{$hash_key{$i}->[0]}); # if you printed the entry from the database delete it , you don't need duplicates. this goes if your record has 80 90 situation or 90 80 situation } __DATA__ A 83 GLU A 90 GLU A 163 ARG A 83 ARG A 222 ARG A 5 ARG A 229 ALA A 115 ALA A 257 ALA A 118 ALA A 328 ASP A 95 ASP A 83 GLU A 90 GLU A 163 ARG A 83 ARG A 222 ARG A 5 ARG A 83 GLU B 90 GLU A 163 ARG B 83 ARG A 222 ARG B 5 ARG