I need to modify a .fasta file that looks like this:
>Contig_1;2
AGATC...
>Contig_2;345
AaGGC...
>Contig_3;22
GGAGA...
And transform it into something like:
>Contig_1
AGATC...
>Contig_2
AaGGC...
>Contig_3
GGAGA...
I tried doing the following, but it did not work as intended.
sed -i 's/;*\n/\n/g' file.fasta
Could someone give me some advice? Thanks!
>Solution :
You can use
sed -i 's/;[^;]*$//' file.fasta
See the online demo:
s='>Contig_1;2
AGATC...
>Contig_2;345
AaGGC...
>Contig_3;22
GGAGA...'
sed 's/;[^;]*$//' <<< "$s"
Output:
>Contig_1
AGATC...
>Contig_2
AaGGC...
>Contig_3
GGAGA...
Note that sed does not place the newline into the pattern space (since you are using a GNU sed, you could force it to do so with -z, but it is not necessary here), and you can’t match a newline with \n in your sed command.
The ;[^;]*$ pattern matches
;– a semi-colon[^;]*– any zero or more chars other than;(if you need to make sure you match digits, replace with[0-9]*or[[:digit:]]*)$– end of string.
Note you need no g flag here since this command needs to perform a single replacement per line.