Code:
awk '{if(len==0){last=$4;total=$6;len=1;getline}if($4!=last){printf("%s\t%f\n", last, total/len);last=$4;total=$6;len=1}else{total+=$6;len+=1}}END{printf("%s\t%f\n", last, total/len)}' output.bam.hist.txt > average.txt
The awk will do the first 2 things (length of bait and average # of reads), but I am not sure how to accomplish the last part (calculate the 40x coverage). Thank you .