Reputation: 3344
I have one problem. I am using this code in bash and awk:
#!/bin/bash
awk 'BEGIN {print "CHR\tSTART\tSTOP\tPOCET_READU\tGCcontent"}'
for z in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
do
export $z
for i in {0..249480000..60000}
do
u=$i
let "u +=60000"
export $i
export $u
samtools view /home/filip/Desktop/AMrtin\ Hynek/highThan89MapQ.bam chr$z:$i-$u | awk '{ n=length($10); print gsub(/[GCCgcs]/,"",$10)/n;}'| awk -v chr="chr"$z -v min=$i -v max=$u '{s+=$1}END{print chr,"\t",min,"\t",max,"\t",NR,"\t",s/NR}}'
done
done
From this I am getting the result like this:
chr1 60000 120000 30 0.333
chr3 540000 600000 10 0.555
The step of loop is 60000, but if I divide s/NR, sometimes the NR is 0 and this row is not in output. Thank I wan to get if the NR=0 and s/NR does not exist (because we cannot divide by 0):
chr1 0 60000 N/A N/A
chr1 60000 120000 30 0.333
chr3 480000 540000 N/A N/A
chr3 540000 600000 10 0.555
I tried use condition like
{s+=$1}END{print chr,"\t",min,"\t",max,"\t",NR,"\t",s/NR; if (S/NR == "") print chr,"\t",min,"\t",max,"\t","N/A","\t","N/A"}'
But it doesnt work.
Could you help me please?
Upvotes: 0
Views: 94
Reputation: 780818
The problem is you're dividing by zero, which is an error. You need to test NR
before doing the division.
awk -v chr="chr"$z -v min=$i -v max=$u '
{s+=$1}
END {print chr, "\t", min, "\t", max, "\t", (NR ? NR : "N/A"), "\t", (NR ? s/NR : "N/A")}'
Upvotes: 1