use Storable; open IN, "input.fa"; my %id2off; while () { next unless /^>/; chomp; $id2off{substr($_, 1)} = tell(IN); # file pointer points to start # of next line } store \%id2off, 'input.idx'; #### 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; } }