removing mitochondrial read and unassembled "random" from multiple bam files This is the one i used for a single bam file to filter its mitochondrial as well as unassembled read 

samtools view -h SRR2920466.bam | awk '{if($3!= "chrM" &amp;&amp; $3 != "chrUn"){print $0}}' | samtools view -Shb - &gt; this.filter.bam 


Is there any way i can do it on multiple files with parallel as i do to index and sort bam files using samtools .

So i tried this ,which is of course not working as its not able to pipe properly because it shows me error regarding additional fonts 

ls *.bam | parallel "samtools view -h {} | awk '{if($3 != "chrM" &amp;&amp; $3 != "chrUn"){print $0}}'| samtools view -Shb {.}.bam" 


Any help with my above codes? As i get it there is wrong with my pipe .

After updating the samtools to version 1.9 i ran the small script 

i get this error 

awk: cmd. line:1: {if( != chrM &amp;&amp;  != chrUn){print ./index_sort.sh}}
awk: cmd. line:1:      ^ syntax error
awk: cmd. line:1: {if( != chrM &amp;&amp;  != chrUn){print ./index_sort.sh}}
awk: cmd. line:1:                                    ^ unterminated regexp
awk: cmd. line:1: {if( != chrM &amp;&amp;  != chrUn){print ./index_sort.sh}}
awk: cmd. line:1:                                                   ^ unexpected newline or end of string
awk: cmd. line:1: {if( != chrM &amp;&amp;  != chrUn){print ./index_sort.sh}}
awk: cmd. line:1:      ^ syntax error
awk: cmd. line:1: {if( != chrM &amp;&amp;  != chrUn){print ./index_sort.sh}}
awk: cmd. line:1:                                    ^ unterminated regexp
awk: cmd. line:1: {if( != chrM &amp;&amp;  != chrUn){print ./index_sort.sh}}
awk: cmd. line:1:                                                   ^ unexpected newline or end of string
awk: cmd. line:1: {if( != chrM &amp;&amp;  != chrUn){print ./index_sort.sh}}
awk: cmd. line:1:      ^ syntax error
awk: cmd. line:1: {if( != chrM &amp;&amp;  != chrUn){print ./index_sort.sh}}
awk: cmd. line:1:                                    ^ unterminated regexp
awk: cmd. line:1: {if( != chrM &amp;&amp;  != chrUn){print ./index_sort.sh}}
awk: cmd. line:1:                                                   ^ unexpected newline or end of string
Your awk command is inside double quotes ("") which means that all of the awk field variables (e.g. $3) are being interpreted by the shell. Since $3 is not set in the shell, your awk is trying to compare an empty string and that's why you get the error:

awk: cmd. line:1: {if( != chrM &amp;&amp;  != chrUn){print ./index_sort.sh}}
awk: cmd. line:1:      ^ syntax error


Note how the error message shows if( != chrM and not if($3 != chrM. Since you need to use double quotes here because you need both an outer (for parallel) and an inner (for awk) quote, and single quotes can't be nested, you will have to escape the awk variables:

ls *.bam | parallel "samtools view -h {} | 
    awk '{if(\$3 != "chrM" &amp;&amp; \$3 != "chrUn"){print \$0}}' | samtools view -Shb {.}.bam"