Replacing a character in multiple fasta files within a folder

Viewed 46

How can I replace a character in multiple fasta files within a folder? More specific, I want to replace the character "?" by "N" through multiple fasta files.

enter image description here

1 Answers

Using to only replace literal question mark characters on non-header lines:

sed -i.bak '/^>/!s/?/N/g' /path/to/fasta/*.fasta

If your FASTA files are gzipped, just use a for loop:

for f in ./fasta/*.fasta.gz ; do
    cp "${f}" "${f}.bak" &&
    zcat "${f}.bak" | sed '/^>/!s/?/N/g' | gzip > "${f}"
done
Related