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

重复的bash变量在命令中只使用一个实例

  •  0
  • justaguy  · 技术社区  · 6 年前

    bash 下面我正在循环通过 .fastq 文件并在注释命令中使用它们。变量 $pre 它的名字在里面,它确实提取了它,我不知道的问题是如何只在注释命令中使用它一次?在下面的示例中 $PRE NA11111 但提取了两次。有没有一种方法只在命令中使用一次?我尝试删除重复项 awk 没有运气和尝试 cut . 谢谢:)。

    猛击

     for file in /home/cmccabe/Desktop/fastq/*.fastq ; do
     sample=${file%.fastq}
     bname=`basename $sample`
     pre="$(echo $bname|cut -d- -f1,1)"
    
    #bwa mem -M -t 16 /home/cmccabe/Desktop/NGS/picard-tools-1.140/resources/ucsc.hg19.fasta "$sample.fastq" "$sample" /home/cmccabe/Desktop/fastq/${pre}_aln.sam
       echo "$sample.fastq"
       echo "$sample"
       echo "$pre"
       done
    

    电流输出

    /home/cmccabe/Desktop/fastq/NA11111-100ng-E08A-C06_S5_L001_R1_001.fastq   `this is $sample.fastq`
    /home/cmccabe/Desktop/fastq/NA11111-100ng-E08A-C06_S5_L001_R1_001         `this is $sample`
    NA11111                                                                   `this is $pre`
    /home/cmccabe/Desktop/fastq/NA11111-100ng-E08A-C06_S5_L001_R2_001.fastq   `this is $sample.fastq`
    /home/cmccabe/Desktop/fastq/NA11111-100ng-E08A-C06_S5_L001_R2_001         `this is $sample`
    NA11111                                                                   `this is $pre`
    

    期望输出

    #bwa mem -M -t 16 /home/cmccabe/Desktop/NGS/picard-tools-1.140/resources/ucsc.hg19.fasta "$sample.fastq" "$sample" /home/cmccabe/Desktop/fastq/${pre}_aln.sam
    
    $sample.fastq = /home/cmccabe/Desktop/fastq/NA11111-100ng-E08A-C06_S5_L001_R1_001.fastq
    $sample = /home/cmccabe/Desktop/fastq/NA11111-100ng-E08A-C06_S5_L001_R1_001
    $pre = NA11111
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   chepner    6 年前

    最简单的方法就是跟踪你已经看到的项目, 如果匹配,则跳过当前文件。

    declare -A seen=()
    
    for file in /home/cmccabe/Desktop/fastq/*.fastq ; do
      sample=${file%.fastq}
      bname=$(basename "$sample")
      pre=${name%%-*}
    
      # Go to the next file if $pre has already been seen
      [[ -v seen[$pre] ]] && continue
    
      # Remember that we've now seen $pre
      seen[$pre]=
    
      bwa mem -M -t 16 /home/cmccabe/Desktop/NGS/picard-tools-1.140/resources/ucsc.hg19.fasta "$sample.fastq" "$sample" "/home/cmccabe/Desktop/fastq/${pre}_aln.sam"
    done
    
        2
  •  1
  •   Poshi    6 年前

    我认为您正在努力实现以下目标:

    for file in /home/cmccabe/Desktop/fastq/*_R1_*.fastq
    do
        file2=$(echo $file | sed 's/_R1_/_R2_/')
        sample=$(basename $file .fastq | cut -d- -f1)
    
        bwa mem -M -t 16 -R "@RG\tID:$sample\tSM:$sample" /home/cmccabe/Desktop/NGS/picard-tools-1.140/resources/ucsc.hg19.fasta $file $file2 > /home/cmccabe/Desktop/fastq/${sample}_aln.sam
    done
    

    在我看来,这是对您的数据最好的常识处理。我假设您将需要两端,并且您将对结果进行后处理,因此需要readgroup行。