Question: I need to reformat headers in a fasta file with headers such as:

>Agaricus_chiangmaiensis|JF514531|SH174817.07FU|reps|k__Fungi;p__Basidiomycota;c__Agaricomycetes;o__Agaricales;f__Agaricaceae;g__Agaricus;s__Agaricus_chiangmaiensis
TTGAATTATGTTTTCTAGATGGGTTGTAGCTGGCTCTTCGGAGCATGTGCACGCCTGCCTGGATTTCATTTTCATCCACCTGTGCACCTATTGTAGTCTCTGTCGGGTATTGAGGAAGTG
>Acarospora_laqueata|DQ842014|SH191965.07FU|refs|k__Fungi;p__Ascomycota;c__Lecanoromycetes;o__Acarosporales;f__Acarosporaceae;g__Acarospora;s__Acarospora_laqueata
TCGAGTTAGGGTCCCTCGGGCCCAACCTCCAACCCTTTGTGTACCTACTTTTGTTGCTTTGGCGGGCCCGCTGGGAAACTCCACCGGCGGCCACAGGCTGCCGAGCGCCCGTCAGA
>Ceratobasidiaceae_sp|DQ493566|SH185440.07FU|reps|k__Fungi;p__Basidiomycota;c__Agaricomycetes;o__Cantharellales;f__Ceratobasidiaceae;g__unidentified;s__Ceratobasidiaceae_sp
TCGAACGAATGTAGAGTCGGTTGTCGCTGGCCCTCTCTGCTGGGCATGTGCACACCTTCTCTTTCATCCACACACACCTGTGCACTCGTGAAGACGGAAGGAGCGCCCTTGGGCGGCGTCC

So that they look like:

>SH174817.07FU Agaricus chiangmaiensis
TTGAATTATGTTTTCTAGATGGGTTGTAGCTGGCTCTTCGGAGCATGTGCACGCCTGCCTGGATTTCATTTTCATCCACCTGTGCACCTATTGTAGTCTCTGTCGGGTATTGAGGAAGTG
>SH191965.07FU Acarospora laqueata
TCGAGTTAGGGTCCCTCGGGCCCAACCTCCAACCCTTTGTGTACCTACTTTTGTTGCTTTGGCGGGCCCGCTGGGAAACTCCACCGGCGGCCACAGGCTGCCGAGCGCCCGTCAGA
>SH185440.07FU Ceratobasidiaceae sp
TCGAACGAATGTAGAGTCGGTTGTCGCTGGCCCTCTCTGCTGGGCATGTGCACACCTTCTCTTTCATCCACACACACCTGTGCACTCGTGAAGACGGAAGGAGCGCCCTTGGGCGGCGTCC

Is there a relatively simple code that can isolate these specific elements and re-order them? I think I can get the first part with something like:

grep -r -o "SH.*FU" file.fasta

But I am unsure how to isolate and reformat the genus and species names in addition to that.

Answer: Given `in.fa`:

$ more in.fa
>Agaricus_chiangmaiensis|JF514531|SH174817.07FU|reps|k__Fungi;p__Basidiomycota;c__Agaricomycetes;o__Agaricales;f__Agaricaceae;g__Agaricus;s__Agaricus_chiangmaiensis
TTGAATTATGTTTTCTAGATGGGTTGTAGCTGGCTCTTCGGAGCATGTGCACGCCTGCCTGGATTTCATTTTCATCCACCTGTGCACCTATTGTAGTCTCTGTCGGGTATTGAGGAAGTG
>Acarospora_laqueata|DQ842014|SH191965.07FU|refs|k__Fungi;p__Ascomycota;c__Lecanoromycetes;o__Acarosporales;f__Acarosporaceae;g__Acarospora;s__Acarospora_laqueata
TCGAGTTAGGGTCCCTCGGGCCCAACCTCCAACCCTTTGTGTACCTACTTTTGTTGCTTTGGCGGGCCCGCTGGGAAACTCCACCGGCGGCCACAGGCTGCCGAGCGCCCGTCAGA
>Ceratobasidiaceae_sp|DQ493566|SH185440.07FU|reps|k__Fungi;p__Basidiomycota;c__Agaricomycetes;o__Cantharellales;f__Ceratobasidiaceae;g__unidentified;s__Ceratobasidiaceae_sp
TCGAACGAATGTAGAGTCGGTTGTCGCTGGCCCTCTCTGCTGGGCATGTGCACACCTTCTCTTTCATCCACACACACCTGTGCACTCGTGAAGACGGAAGGAGCGCCCTTGGGCGGCGTCC

Here's one way:

$ awk '{ if ($0~/^>/) { n=split($0, a, "|"); gsub(/_/," ", a[1]); printf(">%s %s\n", a[3], substr(a[1], 2)); } else { print $0; } }' in.fa
>SH174817.07FU Agaricus chiangmaiensis
TTGAATTATGTTTTCTAGATGGGTTGTAGCTGGCTCTTCGGAGCATGTGCACGCCTGCCTGGATTTCATTTTCATCCACCTGTGCACCTATTGTAGTCTCTGTCGGGTATTGAGGAAGTG
>SH191965.07FU Acarospora laqueata
TCGAGTTAGGGTCCCTCGGGCCCAACCTCCAACCCTTTGTGTACCTACTTTTGTTGCTTTGGCGGGCCCGCTGGGAAACTCCACCGGCGGCCACAGGCTGCCGAGCGCCCGTCAGA
>SH185440.07FU Ceratobasidiaceae sp
TCGAACGAATGTAGAGTCGGTTGTCGCTGGCCCTCTCTGCTGGGCATGTGCACACCTTCTCTTTCATCCACACACACCTGTGCACTCGTGAAGACGGAAGGAGCGCCCTTGGGCGGCGTCC