#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use strict; my$line; my@fields; my@output; 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: $!"; my $position = tell(FILE2); my %rs; while ($line=) { my ($key)= $line=~/^(rs\d{5,})\b/; if (defined $key) { $rs{$key}= $position; } $position=tell(FILE2); } while (defined ($line= )) { my@fields= split (/\s+/ ,$line); push @output, grep /^rs\d{5,}\b/ ,@fields; } print Dumper(\@output,\%rs); foreach (@output) { if (exists $rs{$_}) { seek(FILE2,$rs{$_},0); my $line= ; print FD $line; } } close FILE1; close FILE2; close FD;