The simplest way is to just print the 1st line and then all the other lines of the file that don't contain i) any spaces character (they have no business being in fasta files) and ii) a fasta header line (>
):
head -n 1 file.fa > newfile.fa; grep -P '^[^> ]+$' >> newfile.fa
The head
prints the 1st line, and the grep regular expression looks for all lines that contain only non->
and non-space characters ([^> ]
) from the beginning (^
) to the end ($
) of the line.
However, this will result in a file like this:
>1
AAATTTTGGGGCCC
ACCCCGGGTTT
ATGCCCCCCCCCC
To get the entire sequence on the same line, use this instead:
head -n 1 file.fa > newfile.fa; grep -P '^[^> ]+$' | tr -d '\n'>> newfile.fa;
You can then add an extra newline to the end of the file with
echo "" >> newfile.fa
However, if you will be working with such files, I suggest you save these two scripts in a file in your $PATH ($HOME/bin
for example) and make them executable (chmod a+x $HOME/bin/scriptname
):
FastaToTbl
This script takes a fasta file and changes it to tbl format (the fasta header, a tab and then the sequence, all on one line):
#! /bin/sh
gawk '{
if (substr($1,1,1)==">")
if (NR>1)
printf "\n%s\t", substr($0,2,length($0)-1)
else
printf "%s\t", substr($0,2,length($0)-1)
else
printf "%s", $0
}END{printf "\n"}' "$@"
TblToFasta
This does the inverse, it takes a file in tbl format and converts it to a correct fasta file (>
header and 60 characters per line):
#! /bin/sh
gawk '{
sequence=$NF
ls = length(sequence)
is = 1
fld = 1
# if (fld == 1){printf ">"}
while (fld < NF)
{
if (fld == 1){printf ">"}
printf "%s " , $fld
if (fld == NF-1)
{
printf "\n"
}
fld = fld+1
}
while (is <= ls)
{
printf "%s\n", substr(sequence,is,60)
is=is+60
}
}' "$@"
Now, if you had had these scripts available, you could have done what you asked for by simply running:
$ head -n 1 file.fa; FastaToTbl file.fa | awk -F"\t" '{print $2}'
>1
AAATTTTGGGGCCC
ACCCCGGGTTT..........
ATGCCCCCCCCCC
Or, to get a correct fasta file:
$ head -n 1 file.fa > newfile; FastaToTbl file.fa |
awk -F"\t" '{printf "%s", $2}' | TblToFasta >> newfile
Which produces:
>1
AAATTTTGGGGCCCACCCCGGGTTT..........ATGCCCCCCCCCC