3

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

Zizogolu
  • 2,148
  • 11
  • 44
  • 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 Jan 29 '24 at 11:07

2 Answers2

3

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
Zizogolu
  • 2,148
  • 11
  • 44
-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.

gringer
  • 14,012
  • 5
  • 23
  • 79
  • @zizogolu the brackets go around the entire code, including the for bit – gringer Jan 25 '24 at 17:59
  • @Zizogolu can you please type out what you put into the terminal, and the error messages that were produced? – gringer Jan 27 '24 at 00:15
  • 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. – terdon Jan 29 '24 at 10:47
  • Thanks a million @terdon , your comment solved my issue – Zizogolu Jan 29 '24 at 11:27
  • 2
    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 Jan 29 '24 at 11:44
  • 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 Jan 30 '24 at 05:52