简体   繁体   English

bash 脚本 - 遍历文件并添加到不同的变量

[英]bash script - loop through files and add to different variables

I have files with names like:我有名称如下的文件:

0195_R1.fastq
0195_R2.fastq
0196_R1.fastq
0196_R2.fastq
0197_R1.fastq
0197_R2.fastq

and so on.等等。

I need to run a software for each pair of files (the R1 and R2 are correspondent to each other) like:我需要为每对文件运行一个软件(R1 和 R2 彼此对应),例如:

bowtie2 -x index_files -1 0195_R1.fastq -2 0195_R2.fastq -S 0195_output.sam

With multiple pairs I'd have to run multiple times.对于多对,我必须多次运行。 So I tried to do a bash script using a for loop but I've had no success.所以我尝试使用 for 循环来做一个 bash 脚本,但我没有成功。 Also, I don't know how to rename the output sequentially.另外,我不知道如何按顺序重命名输出。

I've tried the following:我尝试了以下方法:

for R1 in $FQDIR/*_R1.fastq; do
for R2 in $FQDIR/*_R2.fastq; do

    bowtie2 -x index_files -1 $R1 -2 $R2 -S $N_output.sam

done
done

What should I do?我该怎么办?

If you loop over all the R1 and R2 files, you'll run bowtie for all possible pairs of data files.如果您遍历所有 R1 和 R2 文件,您将对所有可能的数据文件对运行bowtie If I understand correctly, that's not what you want - you only want to process the corresponding pairs.如果我理解正确,那不是您想要的-您只想处理相应的对。

To do that, loop over R1 files only, and try to find the corresponding R2 file for each:为此,仅循环 R1 文件,并尝试为每个文件找到相应的 R2 文件:

#!/bin/bash
fqdir=...
for r1 in "$fqdir"/*_R1.fastq; do
    r2=${r1%_R1.fastq}_R2.fastq
    if [[ -f $r2 ]] ; then
        bowtie2 -x index_files -1 "$r1" -2 "$r2" -S "$N"_output.sam
    else
        echo "$r2 not found" >&2
    fi
done

I'm not sure what $N stands for.我不确定$N代表什么。 Maybe you can use $r1 instead?也许你可以用$r1代替?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM