Dear all, I have easy script in bash using awk language. I filtrate bam file by GC content (see at code), but I need to get on output bam file too with header. Could you help me with this? thank you My code:

    #!/bin/bash
    
    for i in 54321*.bam
    do
      samtools view -h /home/filip/Desktop/Analyza\ NIFTY\ and\ CNVs/NIFTY\ pooling009/$i | awk '{ n=length($10); print gsub(/[GCCgcs]/,"",$10)/n,"\t",$0  }' | awk '($1 <= 0.6 && $1 >= 0.3){print $0}' | awk '!($1="")' | samtools -bS - > z$i;
    done
<p>As long as you don&#39;t have any reads beginning with &quot;@&quot; (and you shouldn&#39;t), just add an <code>if(substr($1,1,1) == &quot;@&quot;) { print $0} else {...your current code...}</code>. You just want to avoid all of the piping and have a single awk command (unless you want to modify each of the pipes, which would quickly become annoying).</p>

<p>BTW, this might be easier with pysam. Awk is great, but it can get a bit unwieldy with longer commands.</p>
