How to add specific bases at the beginning and ending of all the fasta sequences?

Hi,
I have to add 7 bases of specific nucleotide at the beginning and ending of all the fasta sequences of a file. For example, I have a multi fasta file namely test.fasta as given below

test.fasta
>TalAA18_Xoo_CIAT_NZ_CP033194.1:_2936369-2939570:+1
ATGTCCCGGACCCGGCTGCCATCTCCCCCTGCGCCCTCGCCTGCGTTCTCGG
>TalAA30_Xoo_PXO421_Pseudo_NZ_CP033189.1:_2340357-2342688:+1
ATGGCGCAATGCACTGA
>TalAA21_Xoo_IX-280_Pseudo_NZ_CP019226.1:_2587862-2590859:-1
ATGGCGCAATGCACTGACGGGTGCCCCCCTGAACCTGACCCCGGACCAAGTGGTGGCCAT

I need to add the following bases CTGA TAGTA at the beginning and ending of every fasta sequences respectively.

Expected outcome
>TalAA18_Xoo_CIAT_NZ_CP033194.1:_2936369-2939570:+1
CTGAATGTCCCGGACCCGGCTGCCATCTCCCCCTGCGCCCTCGCCTGCGTTCTCGGTAGTA
>TalAA30_Xoo_PXO421_Pseudo_NZ_CP033189.1:_2340357-2342688:+1
CTGAATGGCGCAATGCACTGATAGTA
>TalAA21_Xoo_IX-280_Pseudo_NZ_CP019226.1:_2587862-2590859:-1
CTGAATGGCGCAATGCACTGACGGGTGCCCCCCTGAACCTGACCCCGGACCAAGTGGTGGCCATTAGTA

I have tried the following command line to add bases at the end of fasta file, but it is not serving my purpose,

sed 's/$/TAGTA/' test.fasta

. For adding bases at the beginning I do not have any idea, However, I tried the following command

sed -i 's/^/CTGA/' test.fasta

, but its adding bases at the fasta header.
Please help me to do the same.
Thank you.

For exactly the one line FASTA example above, try

sed '/^>/ !{s/^/CTGA/; s/$/TAGTA/}' file
1 Like