Multi-line efetch digging I'm using efetch to dig out a middle &quot;envelope&quot; protein within a viral genome specifically within fasta format. The code below works but may not be robust for an entire database. Thus I want to replace the two &quot;perl&quot; lines with formal code within the Entrez package. I tried using efilter -query but couldn't get it to work. Genome positions are one approach but are not absolute from one virus to another. Rewriting this may be the best option.

ncbi2=NC_002021
efetch -db protein -format gpc -id &quot;$ncbi2" |\
xtract -pattern INSDSeq -element INSDQualifier_value |\
perl -p -e 's/^(.+?)\s+taxon.*.*\s+\b([A-Z]{300}[A-Z]+).*/&gt;$1\n$2/'|\
perl -p -e &quot;s/[\t \;\/]+/_/g&quot;

Output
&gt;InfluenzaAvirus_A_PuertoRico_8_1934
MDVNPTLLFLKVPAQNAISTTFPYTGD.....

I will provide a more detailed explanation shortly.
Any ideas welcome.
Not exactly what you were looking for but may be you can use something like this?
ncbi2=NC_002021
efetch -db protein -format gpc -id &quot;$ncbi2&quot; \
    | xtract -pattern INSDSeq_feature-table \
        -group INSDFeature \
        -if INSDFeature_key -equals 'mat_peptide' \
        -tab '\n' \
        -element INSDFeature_location,INSDQualifier_value \
    | grep 'Segment 2'

For explorations of the INSDSet XMLs, xtract has -insd option that you can use. In this particular case, you can do something like this:
efetch -db nuccore -id &quot;$ncbi2&quot; -format gpc \
    | xtract -insd mat_peptide product peptide

For more information, checkout EDirect documentation and some  example queries