Search code examples
perlshellawksamtools

works on the command line but not in a shell script


I have this line

samtools view -h file | awk '$3=="chr$a" || /^@/' | samtools view -S - -b -o output

the dash between the -S and the -b is supposed to indicate to the program that it is from STDIN. I can run it from a perl script on the command line but as soon as I try to move it into a shell script it just creates the file without outputting any data. Any ideas would be greatly appreciated.


Solution

  • In a shell script the $a inside single quotes will not be expanded:

    for a in {1..22} do 
      samtools view -h AD3.sorted.bam | awk '$3=="chr$a" || /^@/' | samtools view -S - -b -o chr$a.bam 
    done