Answers.Body: <p>Yes, <code>efetch</code> can be given a file with IDs. See <code>efetch -h</code>:</p>
  <pre><code>$ efetch -h | grep -m1 file
    -input         Read identifier(s) from file instead of stdin
  </code></pre>
  <p>So you can pass your file of IDs (e.g. <code>ids</code>) directly:</p>
  <pre><code>$ cat ids
  AB610939
  AB610940
  
  $ efetch -db nuccore -input ids -format fasta
  &gt;AB610939.1 Synthetic construct RNA, spike-in microarray control RNA CRM 6204-a, clone: 6204-a-500-1
  CTCGACTAGTTAATACGGTACAGGATAACCGATCGGCTTGCAACATAACGGCGTTAAGAATGCGGGAGTG
  CAGTTTCCGATTCTCACATCAATCGCCAATAAGGCCTTGTCGCAATATAGACTCAACGGTTCTAGTAGCT
  GATCGGTATTACGTGACGCAACCGATTAGACATGCACAATTCCTTGGTCGCTATACTACGGAAATCGTCA
  GGTACTATAACCCGTCGCAGGCCTAATACGTGTCGTCACATCGCCAACCTATCGTCAGTCGGAAAGACGT
  TGCTGTCTACCATCGAAACTATTTACCGCTCCGAGATTCACGAGTACGAACTCACGAGGAAGTTGCCCTA
  TGTAAGGTATCACTCCAGGTACTGCGCCGATAGTACCAGGTGATCAAACGGTTGCAAGAAGGCCACGACG
  TATCGGGCTCTTTAGACGTACGCTCGAGATTAAACGCGCACTGATTCACTTTAGCCCGGAATGTCTCGGT
  GCGATGTAGA
  &gt;AB610940.1 Synthetic construct RNA, spike-in microarray control RNA CRM 6204-a, clone: CRM6204-a-500-2
  AGACTAAATCTCGGCGTCGGTTCATACGCGCGATCGTTTGCTGTCAGGGCATACTCGAATCCGGACTCCG
  ACAATTATAGGCCATCCTGAATAGCCGATCATGCGAGTCACGATAAGGCAGGCTCTGCGATATCCCGATA
  TACTGGAGAAGCTGAATCCCACCTAGAGCGAACTGTCAGAGGATCGACCTCAGGCTCGCTATCATCATAA
  CGGCGGACGACCTGTGTCACATTCCGAACGCTACGTGACGATATTATCTGTCGAAAGGCATAGAACGCCG
  GTCAATATCCTGCGGCATTCTCTTTATCACCGGCTATAACTACTAGGTTCCGCAGATATAGACTGCGCAC
  GGAACATGTAGATAGATCGAGTAGGGTAGCGATTTAACGACTCGACTTACAGACAGAGACGTAGAACGTC
  AGACGAGTGGTATGCCCACCAGAGGCGATACAGGCTGTACCTGCGTAGCACTAGAGTCGTGCGTCATGCG
  GACCCTATCT
  </code></pre>
  <p>If that were not possible, another option would be to put all your IDs as a comma separated string. If your IDs are in a file named <code>ids</code>, one per line, you can convert that to a comma-separated string like this:</p>
  <pre><code>$ tr '\n' , &lt; ids | sed 's/,$/\n/'
  AB610939,AB610940
  </code></pre>
  <p>That assumes GNU <code>sed</code>. If your <code>sed</code> doesn't handle <code>\n</code>, you can use this instead:</p>
  <pre><code>printf &quot;$(tr '\n' , &lt; ids | sed 's/,$//')&quot; &quot;$(cat ids)&quot;
  </code></pre>
  <p>Armed with this, you can use <a href="https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html" rel="nofollow noreferrer">command substitution</a> to pass the comma-separated list to <code>efetch</code>:</p>
  <pre><code>efetch -db nuccore -id $(tr '\n' , &lt; ids | sed 's/,$/\n/') -format fasta
  </code></pre>
  <p>If there are limits to te number of IDs you can fetch at once, or as may well be the case, if this results in a command that is <a href="https://unix.stackexchange.com/q/120642/22222">too long</a>, then you can use a loop as suggested in <a href="https://bioinformatics.stackexchange.com/a/21587/298">another answer</a>.</p>
  <hr />
  <p>However, be aware that while looping seems to work for <code>efetch</code>, for reasons that are not clear to me, the <code>esearch</code> developers have chosen to make it consume stdin. This means that the obvious, straightforward approach fails with <code>esearch</code>. See <a href="https://unix.stackexchange.com/q/682748/22222">https://unix.stackexchange.com/q/682748/22222</a> for details.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-23T15:01:10.657Z
Answers.LastActivityDate: 2023-09-23T15:01:10.657Z
Answers.UserDisplayName: terdon
Answers.Body: <p>If you're happy looping through each id in turn and each id in the single file (<code>idfile.txt</code>) is separate by a newline:</p>
  <pre><code>while read -r x; do
    efetch -db nuccore -id  &quot;$x&quot; -format fasta
  done &lt; idfile.txt &gt; fetchout.fa 
  </code></pre>
  <p><em>Speculation</em> If the ids are not separated by a newline but a comma on a single line that might give the simultaneous efetch you're wanting. I strongly suspect that each id would need to be surround in quotation marks in such a file. Again this is just speculation and the above solution is preferred IMO.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-23T14:54:34.660Z
Answers.Text: Sorry, my bad. [That issue](https://unix.stackexchange.com/q/682748) seems to be specific to `esearch`, I cannot reproduce it with `efetch`. I deleted my comment.
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-23T16:46:17.810Z
Answers.Text: @terdon world-first from the BioInfo 'nix tools police ;-)
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-23T04:06:23.677Z
Answers.LastActivityDate: 2023-09-23T19:10:24.720Z
Answers.LastEditDate: 2023-09-23T19:10:24.720Z
Answers.UserDisplayName: M__
Body: <p>If I want to efetch two records, I can do the following:</p>
  <pre><code>efetch -db nuccore -id AB610939,AB610940 -format fasta
  </code></pre>
  <p>I have hundreds of records to fetch. Can I put all the IDs in a file and efetch. If so, how?</p>
  
Tags: <edirect>
Title: efetch multiple records at once
ThreadText: # efetch multiple records at once
  
  (Asked by: username_0 on 2023-09-23)
  
  If I want to efetch two records, I can do the following:
  
  ```
  efetch -db nuccore -id AB610939,AB610940 -format fasta
  
  ```
  
  I have hundreds of records to fetch. Can I put all the IDs in a file and efetch. If so, how?
  
  **Tags:** edirect
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-09-23. Score: 3
  
  Yes, `efetch` can be given a file with IDs. See `efetch -h`:
  
  ```
  $ efetch -h | grep -m1 file
    -input         Read identifier(s) from file instead of stdin
  
  ```
  
  So you can pass your file of IDs (e.g. `ids`) directly:
  
  ```
  $ cat ids
  AB610939
  AB610940
  
  $ efetch -db nuccore -input ids -format fasta
  >AB610939.1 Synthetic construct RNA, spike-in microarray control RNA CRM 6204-a, clone: 6204-a-500-1
  CTCGACTAGTTAATACGGTACAGGATAACCGATCGGCTTGCAACATAACGGCGTTAAGAATGCGGGAGTG
  CAGTTTCCGATTCTCACATCAATCGCCAATAAGGCCTTGTCGCAATATAGACTCAACGGTTCTAGTAGCT
  GATCGGTATTACGTGACGCAACCGATTAGACATGCACAATTCCTTGGTCGCTATACTACGGAAATCGTCA
  GGTACTATAACCCGTCGCAGGCCTAATACGTGTCGTCACATCGCCAACCTATCGTCAGTCGGAAAGACGT
  TGCTGTCTACCATCGAAACTATTTACCGCTCCGAGATTCACGAGTACGAACTCACGAGGAAGTTGCCCTA
  TGTAAGGTATCACTCCAGGTACTGCGCCGATAGTACCAGGTGATCAAACGGTTGCAAGAAGGCCACGACG
  TATCGGGCTCTTTAGACGTACGCTCGAGATTAAACGCGCACTGATTCACTTTAGCCCGGAATGTCTCGGT
  GCGATGTAGA
  >AB610940.1 Synthetic construct RNA, spike-in microarray control RNA CRM 6204-a, clone: CRM6204-a-500-2
  AGACTAAATCTCGGCGTCGGTTCATACGCGCGATCGTTTGCTGTCAGGGCATACTCGAATCCGGACTCCG
  ACAATTATAGGCCATCCTGAATAGCCGATCATGCGAGTCACGATAAGGCAGGCTCTGCGATATCCCGATA
  TACTGGAGAAGCTGAATCCCACCTAGAGCGAACTGTCAGAGGATCGACCTCAGGCTCGCTATCATCATAA
  CGGCGGACGACCTGTGTCACATTCCGAACGCTACGTGACGATATTATCTGTCGAAAGGCATAGAACGCCG
  GTCAATATCCTGCGGCATTCTCTTTATCACCGGCTATAACTACTAGGTTCCGCAGATATAGACTGCGCAC
  GGAACATGTAGATAGATCGAGTAGGGTAGCGATTTAACGACTCGACTTACAGACAGAGACGTAGAACGTC
  AGACGAGTGGTATGCCCACCAGAGGCGATACAGGCTGTACCTGCGTAGCACTAGAGTCGTGCGTCATGCG
  GACCCTATCT
  
  ```
  
  If that were not possible, another option would be to put all your IDs as a comma separated string. If your IDs are in a file named `ids`, one per line, you can convert that to a comma-separated string like this:
  
  ```
  $ tr '\n' , < ids | sed 's/,$/\n/'
  AB610939,AB610940
  
  ```
  
  That assumes GNU `sed`. If your `sed` doesn't handle `\n`, you can use this instead:
  
  ```
  printf "$(tr '\n' , < ids | sed 's/,$//')" "$(cat ids)"
  
  ```
  
  Armed with this, you can use [command substitution](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html) to pass the comma-separated list to `efetch`:
  
  ```
  efetch -db nuccore -id $(tr '\n' , < ids | sed 's/,$/\n/') -format fasta
  
  ```
  
  If there are limits to te number of IDs you can fetch at once, or as may well be the case, if this results in a command that is [too long](https://unix.stackexchange.com/q/120642/22222), then you can use a loop as suggested in [another answer](https://bioinformatics.stackexchange.com/a/21587/298).
  
  ---
  
  However, be aware that while looping seems to work for `efetch`, for reasons that are not clear to me, the `esearch` developers have chosen to make it consume stdin. This means that the obvious, straightforward approach fails with `esearch`. See <https://unix.stackexchange.com/q/682748/22222> for details.
  
  
  ---
  
  ### Answer by username_2 on 2023-09-23. Score: 2
  
  If you're happy looping through each id in turn and each id in the single file (`idfile.txt`) is separate by a newline:
  
  ```
  while read -r x; do
    efetch -db nuccore -id  "$x" -format fasta
  done < idfile.txt > fetchout.fa 
  
  ```
  
  *Speculation* If the ids are not separated by a newline but a comma on a single line that might give the simultaneous efetch you're wanting. I strongly suspect that each id would need to be surround in quotation marks in such a file. Again this is just speculation and the above solution is preferred IMO.
  
  > **Comments:**
  > **username_1**: Sorry, my bad. [That issue](https://unix.stackexchange.com/q/682748) seems to be specific to `esearch`, I cannot reproduce it with `efetch`. I deleted my comment.  
  > **username_2**: @username_1 world-first from the BioInfo 'nix tools police ;-)  
  
  ---
  
  