Does anyone know of a simple scripting solution to take a list of accessions and pull out the gene name from the header of FASTA sequences?

For instance given the accession: XP_016469325.1

and given the FASTA entry:
>XP_016469325.1 Nicotiana tabacum|C3H|C3H family protein
MEEELLKRNTDCVYFLASPLTCKKGIECEYRHSEIARLNPRDCWYWLAESCLNPTCAFRH
PPLESHAETSSESAPPQHKSAVPVNKTNVPCYFYFNGYCIKGERCSFLHGPDDGTTTWKS
SKIASGVPDGPTAEKKTSVGSETGPASVEKPSNSSETGSKAAAHEYIKSQVDLISMTNDV
GEQSASHETSGSPSEEATAVRLDSLVPAEGFTQGGSDLSPDWSSDEEVEDNVEREEWLES
SPGFDVLVDDRIEGWSHKDDHSYLLQHDRECDERFAGYDFENNLEYDPAYPDMRIVSDEE
LDDSYYSKVESHEVNEYAREIVIPAHGRQSIPHKRKFPREPGFCARGNVDLRDLLKKRRV
IESDPPNYLSRRLDLSRFNAREQCRDRHRPQGSRWMPQSLASKLESNSSFSSGFVDATRL
EGANQLKKLRQSHRSSYRQQHFKDRRRGRSQPFANETPRRMASRQRSTEVPKIFGGPKTL
AQIREEKIKGREDGNSFERTVPSGGSEREDFSGPKPLSEILKDKRRLSSVVNFSN
 
I would like to have output the gene name "C3H" that is spanned by the "|"

This script I modified from a previous post can grab the gene names, however I'm not sure how to only get the gene names corresponding to a separate list of accessions (accessions.list).

    with open('PlantTFDB_ALL_TF_pep.fas','r') as f:
        for line in f:
            if '>' in line:
                line = line.strip().split('|')
                print(line[1])
File `acc.txt` contains the accession IDs:

    $ cat acc.txt 
    XP_016469325.1

Firstly, getting the mapping relationship between acc and gene name and saving them to acc2gene.tsv

    $ grep '>' seqs.fa  | \
        perl -ne 'next unless />(\S+).+\|(.+?)\|/;   print "$1\t$2\n";'
    XP_016469325.1  C3H

Secondly, joining acc.txt and acc2gene.tsv using csvtk (http://bioinf.shenwei.me/csvtk/download/) or other tools：

    $ csvtk join -H -t -k acc.txt acc2gene.tsv | csvtk cut -H -t -f 2

<hr/>

Original answer

Searching header line using `grep -f `, and capturing accession ID and gene name using regular expression in Perl.

    $ grep '>' seqs.fa  | \
        grep -f acc.txt | \
        perl -ne 'next unless />(\S+).+\|(.+?)\|/;   print "$1\t$2\n";'
    XP_016469325.1  C3H

If you just want the gene names, that would be easier:

    $ grep '>' seqs.fa | grep -f acc.txt | cut -d '|' -f 2
    C3H

