如何在一个循环中使用两个变量

如何在一个循环中使用两个变量

我有两个循环,但我不知道是否可以将它们放在一起:

首先,我合并同一样本中的两个不同 id:

samples="sample01 sample02 sample03"
for s in ${samples}
do
    cat ${s}_id1.txt ${s}_id2.txt > ${s}_id12.txt
done

通过这个,我计算了我的sample.txt中有多少数据是唯一的,范围在19-41之间,并将结果放入count.txt中

for ((t=17, i=1; t<43; t=t+2, i++))
do 
    echo "[$i] Iteration - t = $t"`
    unique.py -o count.txt -t $t sample.txt
done

$t我想要的是为每个 ${s}sample_id12.txt 元素放置不同的值。像这样的东西:

samples="sample01 sample02 sample03"
for s in ${samples} ¿counter?
do
    cat ${s}_id1.txt ${s}_id2.txt > ${s}_id12.txt
    unique.py -o count.txt -t $t ${s}_id12.txt
    rm ${s}_id12.txt
done

答案1

嵌套循环示例

samples="sample01 sample02 sample03"
for s in ${samples}
do
    cat ${s}_id1.txt ${s}_id2.txt > ${s}_id12.txt
    for ((t=17, i=1; t<43; t=t+2, i++))
    do 
        echo "[$i] Iteration - t = $t"
        unique.py -o count.txt -t $t ${s}_id12.txt
    done
done

答案2

for ((t=17; t<43; t=t+2))
do 
    samples="sample01 sample02 sample03"
    for s in ${samples}
    do
        cat ${s}_id1.txt ${s}_id2.txt > ${s}_id12.txt
        unique.py -o count.txt -t $t ${s}_id12.txt
    done
done

这样我就得到了文件t中 17-41 之间的每个文件count.txt(用每个样本文件中每个值对应的值unique.py覆盖结果t

sample01_id12.txt 
sample02_id12.txt
sample03_id12.txt

相关内容