Can you suggest me how I can combine.

a.fasta with 10000 sequences having headers 1, 2, 3, .... 10000 .......................into just one header e.g



  • 1
    Are blank lines in your input? – cuonglm Sep 10 '14 at 12:54
  • I removed the blank lines since your file probably doesn't have them. Please roll back my edit if I was wrong. – terdon Sep 10 '14 at 14:25
awk '
    /^>/ { 
        # print the first header
        if (c++ == 0) {print; print ""} 
    /^$/ {next} 
    {printf "%s", $0} 
    END {print ""}
' a.fasta > b.fasta

contents of b.fasta


  • sorry i am a newbie in this field so do i need to create a file and copy this script and then run or what – star Sep 10 '14 at 13:13
  • copy the command and paste into a shell prompt. – glenn jackman Sep 10 '14 at 13:16
  • i am surprised the commands worked fine but it changes the format of file to txt – star Sep 11 '14 at 13:22
  • If there should not be the blank line after the header, remove ; print "" from the if (c++ ... line. If that's not what you mean, then I don't understand your comment. – glenn jackman Sep 11 '14 at 13:56

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:


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):


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)
            printf "%s\t", substr($0,2,length($0)-1)
                printf "%s", $0
}END{printf "\n"}'  "$@"


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 '{

  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)
}' "$@"

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}'

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:

{ echo '>1'; tr -dc '[:alpha:]'; } <infile >outfile

You dont need to get the first line out of the file - just echo a header. And if you delete everything but alphabetic characters then you just automatically get what you want.

  • Thanks @Braiam - it really wasnt a low-quality post though - it just didnt need a lot of explaining. still i did it. – mikeserv Sep 11 '14 at 2:56

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.