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.
CodePudding user response:
You can increment your own index with something like this:
i=$(( ${i:-0} 1 ))
then your output string can be
"$j_$i"
CodePudding user response:
A simple solution using while read
while read idx fpath; do echo "$(basename $fpath)_$idx.txt"; done <<<$(cat tmp.txt | nl)
Result
a_1.txt
b_2.txt
c_3.txt
For OP's code
bcftools view $j -include 'maf[0]>0.0 & maf[0]<0.005' -output "${fpath}_${idx}"
CodePudding user response:
#!/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 Paramater 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.