Calculating read average length in a Fastq file with bioawk/awk
This can also be done with regular awk.

awk '{if(NR%4==2) {count++; bases += length} } END{print bases/count}' &lt;fastq_file&gt;


The NR%4==2 count the second line out of every block of 4.  length is a built-in that defaults to the length of the line, same as length($0).  In this case you can inject you custom printing to the END{} block but countread and countstored will always be the same since the averaging is done on the fly.