I've got the following script:#!/bin/bashSINGLE=`cut -c 7-21 Data.txt`cd ../FASTA_SEC/for i in ${SINGLE}; do    if [ -r ../FASTA_SEC/${i}.fa ]; then        HEAD=`sed -n 2p ../FASTA_SEC/${i}.fa | head -c 3`        TAIL=`tail -c 4 ../FASTA_SEC/${i}.fa`            if [ ${HEAD} = AAA ]            then                    echo Cut heading A's $i                elif [ ${TAIL} = AAA ]                then                        echo Cut tailing A's $i                    while [ `tail -c 2 ../FASTA_SEC/$i.fa` == A ]                     do                        TRITAIL=`cat ../FASTA_SEC/$i.fa`                        echo ${TRITAIL/A/} > ../FASTA_SEC/$i.fa                    done            fi    else         echo does not exist $i    fidoneIt seems to work in all processed text files including the while loop. But there are a couple of text files where all A's are removed and some spaces are introduced instead of deleting only the tailing A's.I'm quite surprised because it actually works but in some cases it produces a mess. Let me show you an example:Input file which contains A tailing:>B4-0K032_18670_015NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNCNNNGNNNTAGATACAAGCGAGCGGCGGACGGGTGAGTAACACGTGGGTAACCTGCCCAAGAGACTGGGATAACACCTGGAAACAG[Cuted here for shortness]GGNTGTCNTCNGCTNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNAAAAAAAAOutput messed up file:>G4-0K047_18670_010 NNNNNNNNNNCCNCCTGTNNNTTTGCCCCCGGGGGCCTGTCTCTCGGTGTC GTGTCGCCTGGTGGTTCTTCGCGTTGCTTCGTTCCCTGCTCCC[Cuted here for shortness]CGTCCGCCNTCGTTCCTGNTGTCTCGGTGCNNGCCCGTNTNNNNNNNNNN NNNNNNNNNNNNNI want only tailing A's to be cut but in some text files a mess occurs but in most of them it works smoothly. In some files where tailing A's are supposed to be trimmed I get this mess (even other characters might are deleted as well..).I wonder wwy it does work in some cases but in some it does not..Would there be a way to trim tailing A's?
Problem Trimming tailing characters
bash;shell script;string;tail;bioinformatics
The whole script ends up depending of this two lines to remove trailing A's:tritail=$(cat ../FASTA_SEC/$i.fa)echo ${tritail/A/} > ../FASTA_SEC/$i.faSince you are already placing the whole file content in a variable, you do not need a loop to remove all trailing A's. You could just do:tritail=$(cat ../FASTA_SEC/$i.fa)shopt -s extglobecho ${tritail#+(A)} > ../FASTA_SEC/$i.faOr, if you dislike changing the extglob setting:tritail=$(cat ../FASTA_SEC/$i.fa)echo ${tritail%${tritail##*[!A]}} > ../FASTA_SEC/$i.faIn fact, those two commands is all you need to remove trailing A's.The second line works by selecting all the trailing A's. Or, as the command actually does it, by removing everything that is not an A ([!A]) at the leading part of the variable:tail=${tritail##*[!A]}          # Select all the trailing A'sAnd then, the resulting string is removed from the trailing part of the variable:result=${tritail%$tail}       # Remove the trailing A'sBoth parameter expansions are join together into one single command:result=${tritail%${tritail##*[!A]}}And that is what is sent to the (modified) file:echo ${tritail%${tritail##*[!A]}} > ../FASTA_SEC/$i.faTo remove leading A's, switch all the selections:echo ${tritail#${tritail%%[!A]*}} > ../FASTA_SEC/$i.fa