带两个输入文件的For循环 [英] For loop with two input files
问题描述
我希望对需要两个输入文件的命令使用for循环.两个文件之间只有一个字符更改.这是两个示例文件:
I wish to use a for loop for a command that requires two input files. There is a single character change between the two files. Here are two example files:
Fay2_TCCGGAGA-CCTATCCT_L001_R1_001.fastq
Fay2_TCCGGAGA-CCTATCCT_L001_R2_001.fastq
这是我尝试执行的命令:
Here is my attempt of the command:
for f in /directory/*R1*.fastq
pref=${basename "$f" _*R1*.fastq}
command input1 $f input2 ${pref}_*R2*.fastq
问题很可能在于操纵基名称,因此循环使用file1的排列来查找file2.如何使基本名称比整个文件名短.我收到错误
The issue most likely lies in manipulating the basename so the loop uses the permutation of file1 to find file2. How do I make the basename shorter than the entire file name. I am getting the error
Fay2_TCCGGAGA-CCTATCCT_L001_R2_001.fastq_*R2.fastq does not exist
由于下面的注释,循环正常工作,但是每当命令循环一个新文件时,我的文件就会被覆盖.以下是我的整个命令,需要命名两个输出文件.
Thanks to comments below the looping is working properly but my files are being overwritten whenever a the command loops over a new file. The following is my entire command which requires naming both output files.
for f1 in /directory/*R1*.fastq
f2="${f1/_R1_/_R2_}"
pref=${basename "$f" _*R1*.fastq}
command output1 ${pref}_trim_R1.fastq output2 ${pref}_trim_R2.fastq input1 $f input2 $f2
推荐答案
您可以循环浏览R1文件并使用大括号扩展来得出R2文件名:
You can loop through the R1 files and use brace expansion to derive R2 file names:
for r1_file in /directory/*_R1_*.fastq; do
r2_file="${r1_file/_R1_/_R2_}"
command input1 "$r1_file" input2 "$r2_file"
done
这篇关于带两个输入文件的For循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!