This is a test version of Biostars. For the public version, visit https://www.biostars.org.
trim fasta header, remove after hash (#)

this is my fasta file

>BEL-3_Dvir#LINE/BelPao
GTAATTCTTGTGGTAGTTTTATGTTCTATGCAGGTTCAATATCGTACGCGCTCTGTGATG
CCCGCGCAGTACGCGTTTGTTTCCGATAGTTGATAACAAGTAATCGGTACAAATCGATAT
>P-2_Dmel#LINE/P
AATCTCTATATATAAAACTGTTTGTCCTGACTGACTGACTGACTGACTGACTGACTGACT
GACTGATTGGTGATCAACGCACAGCCCAAACCGTAAGAGCTAGGAAGCTGAAATTTTCAC
TGTAGCTACCTTATGTGATGTAGGTGCACGTTAAGACGGGGTTTCGGGAAATTCCACCCG

I want to remove the header after the hash:

>BEL-3_Dvi
GTAATTCTTGTGGTAGTTTTATGTTCTATGCAGGTTCAATATCGTACGCGCTCTGTGATG
CCCGCGCAGTACGCGTTTGTTTCCGATAGTTGATAACAAGTAATCGGTACAAATCGATAT
>P-2_Dme
AATCTCTATATATAAAACTGTTTGTCCTGACTGACTGACTGACTGACTGACTGACTGACT
GACTGATTGGTGATCAACGCACAGCCCAAACCGTAAGAGCTAGGAAGCTGAAATTTTCAC
TGTAGCTACCTTATGTGATGTAGGTGCACGTTAAGACGGGGTTTCGGGAAATTCCACCCG

I tried this code

sed -r -i 's/^>/.*#/\1/' file.fasta

the error is

 -e expression #1, char 10: unknown option to `s'

how to fix this?

header fasta hash trim

also, be extremely careful when using the -i option ( the in-file modification parameter) , if something goes awol you lost your input file. Always work on a copy of the original file or send the sed output to a new file.

1 answer

$ awk '/^>/ {sub(/#.*$/,"",$0)}1' test.fa
$ awk -F "#" '{print $1}' test.fa
$ sed -r '/^>/ s/#.*//' test.fa
$ cut -d '#' -f 1 test.fa

Code you pasted has several issues. Never use -i if you are not sure of code.

Log in to answer this question.