How to convert fasta file to tab delimited file I have a fasta file like 

&gt;sample 1 gene 1
atgc
&gt;sample 1 gene 2
atgc
&gt;sample 2 gene 1 
atgc


I want to get the following output, with one break between the header and the sequence.

&gt;sample 1 gene 1   atgc
&gt;sample 1 gene 2   atgc
&gt;sample 2 gene 1   atgc
If you have multi-line fasta files, as is very common, you can use these scripts1 to convert between fasta and tbl (sequence_name &lt;TAB&gt; sequence) format:


FastaToTbl

#!/usr/bin/awk -f
{
        if (substr($1,1,1)=="&gt;")
      if (NR&gt;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

#! /usr/bin/awk -f
{
  sequence=$NF

  ls = length(sequence)
  is = 1
  fld  = 1
  while (fld &lt; NF)
  {
     if (fld == 1){printf "&gt;"}
     printf "%s " , $fld

     if (fld == NF-1)
      {
        printf "\n"
      }
      fld = fld+1
  }
  while (is &lt;= ls)
  {
    printf "%s\n", substr(sequence,is,60)
    is=is+60
  }
}



Save those in your $PATH, make them executable, and you can then do:

$ cat file.fa
&gt;sequence1 
ATGCGGAGCTTAGATTCTCGAGATCTCGATATCGCGCTTATAAAAGGCCCGGATTAGGGC
TAGCTAGATATCGCGATAGCTAGGGATATCGAGATGCGATACG
&gt;sequence2 
GTACTCGATACGCTACGCGATATTGCGCGATACGCATAGCTAACGATCGACTAGTGATGC
ATAGAGCTAGATCAGCTACGATAGCATCGATCGACTACGATCAGCATCAC
$ FastaToTbl file.fa 
sequence1   ATGCGGAGCTTAGATTCTCGAGATCTCGATATCGCGCTTATAAAAGGCCCGGATTAGGGCTAGCTAGATATCGCGATAGCTAGGGATATCGAGATGCGATACG
sequence2   GTACTCGATACGCTACGCGATATTGCGCGATACGCATAGCTAACGATCGACTAGTGATGCATAGAGCTAGATCAGCTACGATAGCATCGATCGACTACGATCAGCATCAC


And, to get the Fasta back:

$ FastaToTbl file.fa | TblToFasta
&gt;sequence1 
ATGCGGAGCTTAGATTCTCGAGATCTCGATATCGCGCTTATAAAAGGCCCGGATTAGGGC
TAGCTAGATATCGCGATAGCTAGGGATATCGAGATGCGATACG
&gt;sequence2 
GTACTCGATACGCTACGCGATATTGCGCGATACGCATAGCTAACGATCGACTAGTGATGC
ATAGAGCTAGATCAGCTACGATAGCATCGATCGACTACGATCAGCATCAC


This can be a very useful trick when searching a fasta file for a string:

TblToFasta file.fa | grep 'foo' | FastaToTbl




If you really want to keep the leading &gt; of the header (which doesn't seem very useful), you could do something like this:

$ perl -0pe 's/\n//g; s/.&gt;/\n&gt;/g; s/$/\n/;' file.fa 
&gt;sequence1 ATGCGGAGCTTAGATTCTCGAGATCTCGATATCGCGCTTATAAAAGGCCCGGATTAGGGCTAGCTAGATATCGCGATAGCTAGGGATATCGAGATGCGATAC
&gt;sequence2 GTACTCGATACGCTACGCGATATTGCGCGATACGCATAGCTAACGATCGACTAGTGATGCATAGAGCTAGATCAGCTACGATAGCATCGATCGACTACGATCAGCATCAC


But that will read the entire file into memory. If that's an issue, add an empty line between each fasta record, and then use perl's paragraph mode to process each "paragraph" (sequence) at a time:

perl -pe  's/&gt;/\n&gt;/' file.fa | perl -00pe 's/\n//g; s/.&gt;/\n&gt;/g; s/$/\n/;'




1Credit to Josep Abril who wrote these scripts more than a decade ago.