#!/usr/bin/perl -w use strict; my$line; my@fields; my@output; my$position; my%rs; open (FILE1, 'snp.txt') or die "can't open the file: $!"; open (FILE2,'chr22.txt') or die "can't open the file: $!"; open (FD, '>test.txt') or die "can't open the file: $!"; while ($line=) { $line=~/^(rs\d{5,})\b/; $position = tell( FILE2 ); if (defined $line) { $rs{$line}= $position; } } print %rs; while (defined ($line= )) { my@fields= split (/\s+/ ,$line); @output=grep /^rs\d{5,}\b/ ,@fields; } foreach (@output) { if (exists $rs{$_}) { seek(FILE2,$rs{$_},0); my $line= ; print FD $line; } } close FILE1; close FILE2; close FD;