2014-10-29 23 views
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 

等等。

+0

他們沒有,我已經更新了! – colin 2014-10-29 15:57:32

回答

2

你可以使用一個簡單的parameter expansion

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

這將只打印出什麼來執行。如果您對結果滿意,請刪除echo

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表示他的評論我的回答是略小於他的安全,因爲它可以在文件名不正確的位置相匹配(而他是在結束錨定)。

+1

希望不會有名爲'File_somethingR1something_R1.fastq'的文件。 – 2014-10-29 15:56:14