title: extract sequences from multifasta file by ID in file using awk
body: <p>I would like to extract sequences from the multifasta file that match the IDs given by separate list of IDs.</p>
  
  <p>FASTA file seq.fasta:</p>
  
  <pre><code>&gt;7P58X:01332:11636
  TTCAGCAAGCCGAGTCCTGCGTCGTTACTTCGCTT
  CAAGTCCCTGTTCGGGCGCC
  &gt;7P58X:01334:11605
  TTCAGCAAGCCGAGTCCTGCGTCGAGAGTTCAAGTC
  CCTGTTCGGGCGCCACTGCTAG
  &gt;7P58X:01334:11613
  ACGAGTGCGTCAGACCCTTTTAGTCAGTGTGGAAAC
  &gt;7P58X:01334:11635
  TTCAGCAAGCCGAGTCCTGCGTCGAGAGATCGCTTT
  CAAGTCCCTGTTCGGGCGCCACTGCGGGTCTGTGTC
  GAGCG
  &gt;7P58X:01336:11621
  ACGCTCGACACAGACCTTTAGTCAGTGTGGAAATCT
  CTAGCAGTAGAGGAGATCTCCTCGACGCAGGACT
  </code></pre>
  
  <p>IDs file id.txt:</p>
  
  <pre><code>7P58X:01332:11636
  7P58X:01334:11613
  </code></pre>
  
  <p>I want to get the fasta file with only those sequences matching the IDs in the id.txt file:</p>
  
  <pre><code>&gt;7P58X:01332:11636
  TTCAGCAAGCCGAGTCCTGCGTCGTTACTTCGCTTT
  CAAGTCCCTGTTCGGGCGCC
  &gt;7P58X:01334:11613
  ACGAGTGCGTCAGACCCTTTTAGTCAGTGTGGAAAC
  </code></pre>
  
  <p>I really like the awk approach I found in answers <a href="https://stackoverflow.com/questions/26144692/printing-a-sequence-from-a-fasta-file">here</a> and <a href="https://stackoverflow.com/questions/38491388/use-awk-to-search-through-fasta-file-given-a-second-file-containing-sequence-na">here</a>, but the code given there is still not working perfectly for the example I gave. Here is why:</p>
  
  <p>(1)</p>
  
  <pre><code>awk -v seq="7P58X:01332:11636" -v RS='&gt;' '$1 == seq {print RS $0}' seq.fasta
  </code></pre>
  
  <p>this code works well for the multiline sequences but IDs have to be inserted separately to the code.</p>
  
  <p>(2)</p>
  
  <pre><code>awk 'NR==FNR{n["&gt;"$0];next} f{print f ORS $0;f=""} $0 in n{f=$0}' id.txt seq.fasta
  </code></pre>
  
  <p>this code can take the IDs from the id.txt file but returns only the first line of the multiline sequences.</p>
  
  <p>I guess that the good thing would be to modify the RS variable in the code (2) but all of my attempts failed so far. Can, please, anybody help me with that?</p>