Answers.Body: <p>This command worked just by adding  <code>&gt; file</code> after <code>done</code></p>
  <pre><code>for i in <span class="math-container">$(cat patient_id.txt); do 
  dx find data --property eid=$</span>i --path 'vcf/'  
  done &gt; vcf_list_file.txt
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-29T11:51:49.983Z
Answers.LastActivityDate: 2024-01-29T22:30:52.487Z
Answers.LastEditDate: 2024-01-29T22:30:52.487Z
Answers.UserDisplayName: Zizogolu
Answers.Body: <p>Things that write to standard output on the terminal can be wrapped up in brackets and redirected to a file. Assuming this is what you want to do, I added brackets around the existing code, then used <code>&gt;</code> to redirect the output to a file.</p>
  <pre><code>(for i in $(cat patient_id.txt); do
   dx find data --property patient_id=$i --path 'vcf/'  
  done) &gt; vcf_files.txt
  </code></pre>
  <p>Note that the brackets go around the entire code, including the <code>for</code> bit.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-25T17:59:20.443Z
Answers.Text: @zizogolu the brackets go around the entire code, including the `for` bit
Answers.UserDisplayName: gringer
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-27T00:15:47.273Z
Answers.Text: @Zizogolu can you please type out what you put into the terminal, and the error messages that were produced?
Answers.UserDisplayName: gringer
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-29T10:47:52.140Z
Answers.Text: There is no need to make this run in a subshell (that's what the parentheses do). All you need is the redirect in the end. `for i in ...; do ...; done > file` works and redirects all stdout of the for loop. Compare `for i in a b; do echo "$i"; done > file` and the same thing with parentheses, it makes no difference other than running the whole thing in a subshell.
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-29T11:27:02.710Z
Answers.Text: Thanks a million @terdon , your comment solved my issue
Answers.UserDisplayName: Zizogolu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-29T11:44:29.650Z
Answers.Text: That is... odd, @Zizogolu. If it works with `for ...; do ... done > file` then it would also work with Gringer's `(for ...; do ... done) > file`, the parentheses are just irrelevant, they wouldn't stop the redirection from working. In any case, can you please post what worked for you as an answer so the question can be marked as answered?
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-30T05:52:52.150Z
Answers.Text: I suspect the issue was in the `path` command-line argument, which seems to have changed since I copied the code. I've updated that now.
Answers.UserDisplayName: gringer
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-25T09:43:41.500Z
Answers.LastActivityDate: 2024-01-30T05:52:49.263Z
Answers.LastEditDate: 2024-01-30T05:52:49.263Z
Answers.UserDisplayName: gringer
Body: <p>I have a for loop which prints available vcf files in the path for a list of patient IDs <strong>(using find dx data function)</strong> into my zsh terminal:</p>
  <pre><code>for i in $(cat patient_id.txt); do
   dx find data --property patient_id=$i --path &quot;vcf/&quot;  
  done
  </code></pre>
  <p>How I can have a file contain the vcf files that the loop shows in terminal? I mean by using this command in the terminal, I am seeing the name and path of my vcf files for a given participant list and working directory; Instead of seeing the file name in the terminal, I want to have that file name in a txt file.</p>
  <p>Thanks</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-01-29T11:07:14.763Z
Comments.Text: Please [edit] your question and explain what happens if you simply add `> file` after the `done`. Is output redirected to `file`? Is it still shown in your terminal? What is the output? Are you sure files are found and you aren't just getting error messages?
Comments.UserDisplayName: terdon
Tags: <bash><dnanexus>
Title: Printing the results of for loop into a txt file
ThreadText: # Printing the results of for loop into a txt file
  
  (Asked by: username_0 on 2024-01-24)
  
  I have a for loop which prints available vcf files in the path for a list of patient IDs **(using find dx data function)** into my zsh terminal:
  
  ```
  for i in $(cat patient_id.txt); do
   dx find data --property patient_id=$i --path "vcf/"  
  done
  
  ```
  
  How I can have a file contain the vcf files that the loop shows in terminal? I mean by using this command in the terminal, I am seeing the name and path of my vcf files for a given participant list and working directory; Instead of seeing the file name in the terminal, I want to have that file name in a txt file.
  
  Thanks
  
  **Tags:** bash, dnanexus
  
  > **Comments:**
  > **username_1**: Please [edit] your question and explain what happens if you simply add `> file` after the `done`. Is output redirected to `file`? Is it still shown in your terminal? What is the output? Are you sure files are found and you aren't just getting error messages?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2024-01-29. Score: 3 | Accepted answer
  
  This command worked just by adding `> file` after `done`
  
  ```
  for i in $(cat patient_id.txt); do 
  dx find data --property eid=$i --path 'vcf/'  
  done > vcf_list_file.txt
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2024-01-25. Score: -1
  
  Things that write to standard output on the terminal can be wrapped up in brackets and redirected to a file. Assuming this is what you want to do, I added brackets around the existing code, then used `>` to redirect the output to a file.
  
  ```
  (for i in $(cat patient_id.txt); do
   dx find data --property patient_id=$i --path 'vcf/'  
  done) > vcf_files.txt
  
  ```
  
  Note that the brackets go around the entire code, including the `for` bit.
  
  > **Comments:**
  > **username_2**: @username_0 the brackets go around the entire code, including the `for` bit  
  > **username_2**: @username_0 can you please type out what you put into the terminal, and the error messages that were produced?  
  > **username_1**: There is no need to make this run in a subshell (that's what the parentheses do). All you need is the redirect in the end. `for i in ...; do ...; done > file` works and redirects all stdout of the for loop. Compare `for i in a b; do echo "$i"; done > file` and the same thing with parentheses, it makes no difference other than running the whole thing in a subshell.  
  > **username_0**: Thanks a million @username_1 , your comment solved my issue  
  > **username_1**: That is... odd, @username_0. If it works with `for ...; do ... done > file` then it would also work with username_2's `(for ...; do ... done) > file`, the parentheses are just irrelevant, they wouldn't stop the redirection from working. In any case, can you please post what worked for you as an answer so the question can be marked as answered?  
  > **username_2**: I suspect the issue was in the `path` command-line argument, which seems to have changed since I copied the code. I've updated that now.  
  
  ---
  
  