This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Removing space form fasta file header

Could anyone suggest me the easier way (linux command line) to remove space(S) from header of the fasta file?

sequence

Hello kudzu!

Questions similar to yours can already be found at:

We have closed your question to allow us to keep similar content in the same thread.

If you disagree with this please tell us why in a reply below. We'll be happy to talk about it.

Cheers!

PS: 135035
perl -lane "s/\s+//g if /^>/; print " file.fa > newfile.fa
perl -lane "s/\s+/_/g if /^>/; print " file.fa > newfile.fa
perl -lane "s/\s+/./g if /^>/; print " file.fa > newfile.fa

1 answer

This has been answered before: How to remove space in headers of fasta files

Since FASTA should not contain spaces anywhere else than in header, removing all spaces is the easiest/fastest way:

tr -d ' ' <in.fa >out.fa  # remove all spaces
tr ' ' '_' <in.fa >out.fa # replace spaces by _

If you want to change headers only:

sed '/^>/{s/ /_/g}' <in.fa >out.fa 
sed '/^>/{s/ \+/_/}' # replace only first occurance of (multiple) spaces
sed '/^>/{s/\s/_/g}' # \s will also target tabs

That was very quick and easy command to remove all spaces. Thank you very much for your quick response thackl (Y)

Log in to answer this question.