open FILE1,"1.txt" or die "can't open file 1"; open FILE2,"2.txt" or die "can't open file 2"; open(w1,">3.txt"); open(w2,">4.txt"); while (my $line1 = ) { chomp $line1; my @cond1 = split("\t" , $line1); print "$cond1[2]\n"; while (my $line2 = ) { chomp $line2; my @cond2 = split("\t" , $line2); print "$cond2[2]\n"; if(abs($cond1[2]-$cond2[2])>=2) { print w1 "$line1\n"; print w2 "$line2\n"; } } } close FILE1; close FILE2; close w1; close w2; #### file 1 (1.txt) aqw dfr 34 poilo ggg 98 file 2 (2.txt) qww asd 28 poilo ggg 97 #### 3.txt aqw dfr 34 4.txt qww asd 28