I've got done the following parameter shell expansion:

trihead="$(cat ../FASTA_SEC/"$i".fa)"
echo "${trihead#"${trihead%%[!A]*}"}" > ../FASTA_SEC/"$i".fa

I'd like to trim the leading A's from the second line of a fasta file (a kind of plain text format .fa)

the input would be something like:


and i'd like the output to be like:


By the way, does anybody know some side where shell parameter expansion is well explained?

  • 2
    Please edit your question and explain i) what the command is supposed to be doing and ii) what it is actually doing. Also, this isn't a bioinformatics site, people here have no idea what FASTA is. Please include an example of your input file and show us the output you would like to see.
    – terdon
    Jan 13, 2017 at 17:19
  • 1
    Don't read a fasta file into a variable. It looks more like this is a job for sed or awk, depending on what it is you're trying to achieve on that second line.
    – Kusalananda
    Jan 13, 2017 at 17:23
  • Edited sorry! I though about sed sth like sed -e '4 {all leading A's}' > file but iunno how to order sed to delete leading A's from 2nd line not all A's or so..
    – Neuls
    Jan 13, 2017 at 17:33

2 Answers 2


To trim the leading consecutive As out of the second line of a file (and then replace the original file with the result):

$ sed '2s/^A*//' data.fa >data.out  &&  mv data.out data.fa

The sed command applies the substitution command (s) to line 2 specifically in this example. It will substitute any number of As at the start of the line with nothing.

The pattern is ^A* which means "match zero or more (*) of the single character A at the start of the line (^)".

The reason I don't use sed -i (for "in-place editing") is that the -i flag is horribly non-portable between sed implementations.

  • so to order A leading characters do u use ^ ? for trimming tailing A's you do sed '/^N*//g' file
    – Neuls
    Jan 13, 2017 at 17:44
  • ^ is the regular expression anchor for "Start of the line".
    – DopeGhoti
    Jan 13, 2017 at 17:45
  • @Neuls The ^ is called an "anchor". It anchors the pattern (A*) to the start of the line. The anchor for the end or the line is $.
    – Kusalananda
    Jan 13, 2017 at 17:45

This will remove all leading As from a line following a line that start with >:

sed '/^>/n;s/^A*//' /path/to/file

If the output is what you want, and you want to edit the original file, use:

sed --in-place '/^>/n;s/^A*//' /path/to/file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.