title: Sorting fasta file by chromosome in a specific and pre-defined orders
body: <p>I have a fasta file that looks like the following:</p>
  <pre><code>  &gt;chr1
      NNNNNNNNNNNNNNNNNNNNNNNN
      NNNNNNNNNNNNNNNNNNNNNNNN
      .....
    &gt;chr2
      NNNNNNNNNNNNNNNNNNNNNNNN
      NNNNNNNNNNNNNNNNNNNNNNNN
      .....
  </code></pre>
  <p>I am trying to sort the file by the chromosome number exactly like the following:</p>
  <pre><code>&gt;chr1
  &gt;chr2   
  &gt;chr3   
  .
  .
  .
  &gt;chr22
  &gt;chrX
  &gt;chrY
  &gt;chrM
  </code></pre>
  <p>However, I have the following orders:</p>
  <pre><code>&gt;chr1
  &gt;chr1_KI270706v1_random
  &gt;chr1_KI270707v1_random
  &gt;chr1_KI270708v1_random
  &gt;chr1_KI270709v1_random
  &gt;chr1_KI270710v1_random
  &gt;chr1_KI270711v1_random
  &gt;chr1_KI270712v1_random
  &gt;chr1_KI270713v1_random
  &gt;chr1_KI270714v1_random
  &gt;chr2
  &gt;chr2_KI270715v1_random
  &gt;chr2_KI270716v1_random
  &gt;chr3
  &gt;chr3_GL000221v1_random
  &gt;chr4
  &gt;chr4_GL000008v2_random
  &gt;chr5
  &gt;chr5_GL000208v1_random
  &gt;chr6
  &gt;chr7
  &gt;chr8
  &gt;chr9
  &gt;chr9_KI270717v1_random
  &gt;chr9_KI270718v1_random
  &gt;chr9_KI270719v1_random
  &gt;chr9_KI270720v1_random
  &gt;chr10
  &gt;chr11
  &gt;chr11_KI270721v1_random
  &gt;chr12
  &gt;chr13
  &gt;chr14
  &gt;chr14_GL000009v2_random
  &gt;chr14_GL000194v1_random
  &gt;chr14_GL000225v1_random
  &gt;chr14_KI270722v1_random
  &gt;chr14_KI270723v1_random
  &gt;chr14_KI270724v1_random
  &gt;chr14_KI270725v1_random
  &gt;chr14_KI270726v1_random
  &gt;chr15
  &gt;chr15_KI270727v1_random
  &gt;chr16
  &gt;chr16_KI270728v1_random
  &gt;chr17
  &gt;chr17_GL000205v2_random
  &gt;chr17_KI270729v1_random
  &gt;chr17_KI270730v1_random
  &gt;chr18
  &gt;chr19
  &gt;chr20
  &gt;chr21
  &gt;chr22
  &gt;chr22_KI270731v1_random
  &gt;chr22_KI270732v1_random
  &gt;chr22_KI270733v1_random
  &gt;chr22_KI270734v1_random
  &gt;chr22_KI270735v1_random
  &gt;chr22_KI270736v1_random
  &gt;chr22_KI270737v1_random
  &gt;chr22_KI270738v1_random
  &gt;chr22_KI270739v1_random
  &gt;chrEBV
  &gt;chrM
  &gt;chrUn_GL000195v1
  &gt;chrUn_GL000213v1
  &gt;chrUn_GL000214v1
  &gt;chrUn_GL000216v2
  &gt;chrUn_GL000218v1
  &gt;chrUn_GL000219v1
  &gt;chrUn_GL000220v1
  &gt;chrUn_GL000224v1
  &gt;chrUn_GL000226v1
  &gt;chrUn_KI270302v1
  &gt;chrUn_KI270303v1
  &gt;chrUn_KI270304v1
  &gt;chrUn_KI270305v1
  &gt;chrUn_KI270310v1
  &gt;chrUn_KI270311v1
  &gt;chrUn_KI270312v1
  &gt;chrUn_KI270315v1
  &gt;chrUn_KI270316v1
  &gt;chrUn_KI270317v1
  &gt;chrUn_KI270320v1
  &gt;chrUn_KI270322v1
  &gt;chrUn_KI270329v1
  &gt;chrUn_KI270330v1
  &gt;chrUn_KI270333v1
  &gt;chrUn_KI270334v1
  &gt;chrUn_KI270335v1
  &gt;chrUn_KI270336v1
  &gt;chrUn_KI270337v1
  &gt;chrUn_KI270338v1
  &gt;chrUn_KI270340v1
  &gt;chrUn_KI270362v1
  &gt;chrUn_KI270363v1
  &gt;chrUn_KI270364v1
  &gt;chrUn_KI270366v1
  &gt;chrUn_KI270371v1
  &gt;chrUn_KI270372v1
  &gt;chrUn_KI270373v1
  &gt;chrUn_KI270374v1
  &gt;chrUn_KI270375v1
  &gt;chrUn_KI270376v1
  &gt;chrUn_KI270378v1
  &gt;chrUn_KI270379v1
  &gt;chrUn_KI270381v1
  &gt;chrUn_KI270382v1
  &gt;chrUn_KI270383v1
  &gt;chrUn_KI270384v1
  &gt;chrUn_KI270385v1
  &gt;chrUn_KI270386v1
  &gt;chrUn_KI270387v1
  &gt;chrUn_KI270388v1
  &gt;chrUn_KI270389v1
  &gt;chrUn_KI270390v1
  &gt;chrUn_KI270391v1
  &gt;chrUn_KI270392v1
  &gt;chrUn_KI270393v1
  &gt;chrUn_KI270394v1
  &gt;chrUn_KI270395v1
  &gt;chrUn_KI270396v1
  &gt;chrUn_KI270411v1
  &gt;chrUn_KI270412v1
  &gt;chrUn_KI270414v1
  &gt;chrUn_KI270417v1
  &gt;chrUn_KI270418v1
  &gt;chrUn_KI270419v1
  &gt;chrUn_KI270420v1
  &gt;chrUn_KI270422v1
  &gt;chrUn_KI270423v1
  &gt;chrUn_KI270424v1
  &gt;chrUn_KI270425v1
  &gt;chrUn_KI270429v1
  &gt;chrUn_KI270435v1
  &gt;chrUn_KI270438v1
  &gt;chrUn_KI270442v1
  &gt;chrUn_KI270448v1
  &gt;chrUn_KI270465v1
  &gt;chrUn_KI270466v1
  &gt;chrUn_KI270467v1
  &gt;chrUn_KI270468v1
  &gt;chrUn_KI270507v1
  &gt;chrUn_KI270508v1
  &gt;chrUn_KI270509v1
  &gt;chrUn_KI270510v1
  &gt;chrUn_KI270511v1
  &gt;chrUn_KI270512v1
  &gt;chrUn_KI270515v1
  &gt;chrUn_KI270516v1
  &gt;chrUn_KI270517v1
  &gt;chrUn_KI270518v1
  &gt;chrUn_KI270519v1
  &gt;chrUn_KI270521v1
  &gt;chrUn_KI270522v1
  &gt;chrUn_KI270528v1
  &gt;chrUn_KI270529v1
  &gt;chrUn_KI270530v1
  &gt;chrUn_KI270538v1
  &gt;chrUn_KI270539v1
  &gt;chrUn_KI270544v1
  &gt;chrUn_KI270548v1
  &gt;chrUn_KI270579v1
  &gt;chrUn_KI270580v1
  &gt;chrUn_KI270581v1
  &gt;chrUn_KI270582v1
  &gt;chrUn_KI270583v1
  &gt;chrUn_KI270584v1
  &gt;chrUn_KI270587v1
  &gt;chrUn_KI270588v1
  &gt;chrUn_KI270589v1
  &gt;chrUn_KI270590v1
  &gt;chrUn_KI270591v1
  &gt;chrUn_KI270593v1
  &gt;chrUn_KI270741v1
  &gt;chrUn_KI270742v1
  &gt;chrUn_KI270743v1
  &gt;chrUn_KI270744v1
  &gt;chrUn_KI270745v1
  &gt;chrUn_KI270746v1
  &gt;chrUn_KI270747v1
  &gt;chrUn_KI270748v1
  &gt;chrUn_KI270749v1
  &gt;chrUn_KI270750v1
  &gt;chrUn_KI270751v1
  &gt;chrUn_KI270752v1
  &gt;chrUn_KI270753v1
  &gt;chrUn_KI270754v1
  &gt;chrUn_KI270755v1
  &gt;chrUn_KI270756v1
  &gt;chrUn_KI270757v1
  &gt;chrX
  &gt;chrY
  &gt;chrY_KI270740v1_random
  </code></pre>
  <p>So my aim is to sort the headers like this:</p>
  <pre><code>&gt;chr1
  &gt;chr2   
  &gt;chr3   
  .
  .
  .
  &gt;chr22
  &gt;chrX
  &gt;chrY
  &gt;chrM
  </code></pre>
  <p>and the remaining headers are sorted after the above order, how can I accomplish this?</p>
  <p>I tried different technique but non actually works.</p>
  <p>For example:</p>
  <pre><code>bioawk -c fastx '{print}' in.fa | sort -k1,1V | awk '{print &quot;&gt;&quot;$1;print $2}'
  </code></pre>
  <p>but that did not work.</p>
  <p>Thanks so much in advance.</p>