代码之家  ›  专栏  ›  技术社区  ›  Workhorse

嵌套for循环输入终端

  •  1
  • Workhorse  · 技术社区  · 7 年前

    我有一个包含31个不同文件的目录,每个文件代表一个cell_类型(这些文件的示例如下所示)。

    topControlGenes.GeneSet                          
    topProjection_Cluster5Genes.GeneSet
    topAstrocytesGenes.GeneSet                       
    topProjection_Cluster6Genes.GeneSet
    topNeuronsInhCGE1Genes.GeneSet  
    topNeuronsInhCGE2Genes.GeneSet      
    

    我有一个脚本,为每个cell_类型的文件生成22个新文件(所以31*22=682个文件)。

    for i in `seq 1 22`; do python make_annot.py --gene-set-file CELL_TYPEGenes.GeneSet  --bimfile ../../1000G_EUR_Phase3_plink/1000G.EUR.QC.${i}.bim --annot-file CELL_TYPE.${i}.annot.gz;done
    

    这对每个文件都很有效,但我不希望每次脚本完成时都更改命令中的名称。相反,我想对目录中的每个文件运行此命令。

    有办法吗?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Ollin Boer Bohan    7 年前

    下面是一个使用嵌套for循环的可能解决方案:

    # Iterate over all GeneSet files
    for f in *.GeneSet; do
        # Figure out the cell type by removing "Genes.GeneSet" from the end of the filename
        cell_type=${f%Genes.GeneSet}
        # Process the file
        echo "Processing file: $f with cell type $cell_type"
        for i in {1..22}; do
            python make_annot.py --gene-set-file "$f"  --bimfile ../../1000G_EUR_Phase3_plink/1000G.EUR.QC.${i}.bim --annot-file ${cell_type}.${i}.annot.gz
        done
    done 
    
        2
  •  2
  •   Allan    7 年前

    您可以使用以下嵌套 for 循环:

    for f in *Genes.GeneSet 
    do 
        for i in `seq 1 22`
        do 
            python make_annot.py --gene-set-file "${f}"  --bimfile ../../1000G_EUR_Phase3_plink/1000G.EUR.QC.${i}.bim --annot-file `echo "${f}" | grep -oP '^.*(?=Genes.Geneset$)'`.${i}.annot.gz; 
        done
    done
    

    这会让你的 CELL_TYPEGenes.GeneSet 你的 CELL_TYPE.${i}.annot.gz 产生 31 * 22 = 682 files 组合

    在哪里? echo "${f}" | grep -oP '^.*(?=Genes.Geneset$)' 用于获取文件名的单元格类型部分