use Storable; $id2off = retrieve 'input.idx'; open IN, 'input.fa'; while (<>) { chomp; next unless exists $id2off->{$_}; print ">$_\n"; seek(IN, $id2off->{$_}, 0); # move file pointer to start of sequence while () { # print until we reach next sequence last if /^>/; print; } }