Adding an R solution for people who hate the speed of awk!
library(Biostrings)
library(dplyr)
fasta <- readDNAStringSet(filepath = 'test.fa', format="fasta")
names(fasta)
##[1] "gi|556503834|ref|NC_000913.3|Escherichia coli str. blah blah FIRST SEQ"
##[2] "gi|556503834|ref|NC_000913.3|Escherichia coli str. blah blah SECOND SEQ"
##[3] "gi|556503834|ref|NC_000913.3|Escherichia coli str. blah blah THIRD SEQ"
names(fasta) <-
names(fasta) %>%
strsplit(., split="|",fixed=TRUE) %>%
sapply(., '[', 5) %>%
gsub(" ", "_",.)
names(fasta)
##[1] "Escherichia_coli_str._blah_blah_FIRST_SEQ"
##[2] "Escherichia_coli_str._blah_blah_SECOND_SEQ"
##[3] "Escherichia_coli_str._blah_blah_THIRD_SEQ"
writeXStringSet(fasta, filepath = 'test_EDITED.fa',format="fasta")
Always mention what you've tried. Your questions suggests that you just want an answer and are not interested in learning how to get there, which should not be how anyone approaches this.