<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>

CC BY-SA 4.0
2024-01-29T11:51:49.983Z
2024-01-29T22:30:52.487Z
2024-01-29T22:30:52.487Z
Zizogolu
<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>

CC BY-SA 4.0
2024-01-25T17:59:20.443Z
@zizogolu the brackets go around the entire code, including the `for` bit
gringer
CC BY-SA 4.0
2024-01-27T00:15:47.273Z
@Zizogolu can you please type out what you put into the terminal, and the error messages that were produced?
gringer
CC BY-SA 4.0
2024-01-29T10:47:52.140Z
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.
terdon
CC BY-SA 4.0
2024-01-29T11:27:02.710Z
Thanks a million @terdon , your comment solved my issue
Zizogolu
CC BY-SA 4.0
2024-01-29T11:44:29.650Z
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?
terdon
CC BY-SA 4.0
2024-01-30T05:52:52.150Z
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.
gringer
CC BY-SA 4.0
2024-01-25T09:43:41.500Z
2024-01-30T05:52:49.263Z
2024-01-30T05:52:49.263Z
gringer
<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>

CC BY-SA 4.0
2024-01-29T11:07:14.763Z
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?
terdon
<bash><dnanexus>
Printing the results of for loop into a txt file
# 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**: @zizogolu the brackets go around the entire code, including the `for` bit  
> **username_2**: @Zizogolu 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 @terdon , your comment solved my issue  
> **username_1**: 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?  
> **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.  

---

