Issue
I have a text file that contains a list of file paths. I want to loop through each line (file path), manipulate the file and store the manipulated file under a new name.
To do this I would like dynamically name the new file under file_$index so that each new file gets saved and not overwritten. Any ideas how to do this? my current code is:
for j in $(cat access.txt); do bcftools view $j -include 'maf[0]>0.0 & maf[0]<0.005' -output [FILE_NAME] ; done
i do not know how to dynamically change the file name i.e. to be file_$index. This would be equivalent of doing enumerate on a for loop in python. Note I cannot use the existing file path as that will overwrite the existing file which I do not want
In an ideal world i would manipulate the file path ($j) to extract part of the path as a new name. however I am not sure this is possible so file_$index also works.
Solution
#!/usr/bin/env bash
index=1
while IFS= read -ru3 j; do
bcftools view "$j" -include 'maf[0]>0.0 & maf[0]<0.005' -output "${j}_$index"
((index++))
done 3< access.txt
In an ideal world i would manipulate the file path ($j) to extract part of the path as a new name.
As for the "$j"
a Parameater Expansion can be use to manipulate/extract/replace whatever you want/wish with that variable.
-ru3
is a shorthand for-r
-u 3
It is using the FD3
just in casebcftools
is eating stdin , Seehelp read
((index++))
is an Arithmetic expression in bash.
Answered By - Jetchisel Answer Checked By - Timothy Miller (WPSolving Admin)