1

假设我有一个命令 command.py,它将文件配对File_01_R1.fastqFile_01_R2.fastq. 在单对上执行的命令如下所示:

command.py -f File_01_R1.fastq -r File_01_R2.fastq

但是,我有很多文件,每个文件都有 R1 和 R2 版本。我怎么能告诉这个命令遍历我拥有的每个文件,所以它也执行

command.py -f File_02_R1.fastq -r File_02_R2.fastq
command.py -f File_03_R1.fastq -r File_03_R2.fastq

等等。

4

2 回答 2

2

您可以使用简单的参数扩展

for f in *_R1.fastq; do
    echo command.py -f "$f" -r "${f%_R1.fastq}_R2.fastq"
done

这只会打印出要执行的内容。echo如果您对结果满意,请删除。

于 2014-10-29T15:52:53.760 回答
2
# Loop over all R1.fastq files
for f in File_*_R1.fastq; do
    # Replace R1 with R2 in the filename and run the command on both files.
    command.py -f "$f" -r "${f/_R1./_R2.}"
done; unset -v f

正如@gniourf_gniourf 在他的评论中指出的那样,我的答案比他的安全性稍差,因为它可能在文件名中的错误位置匹配(而他的锚定在最后)。

于 2014-10-29T15:53:02.533 回答