Calculating read average length in a Fastq file with bioawk/awk I found here this awk script: 

BEGIN {
  headertype="";
  }
{
  if($0 ~ "^@") {
    countread++;
    headertype="@";
  }
  else if($0 ~ "^+") {
    headertype="+";
  }
  else if(headertype="@") { # This is a nuc sequence
    len=length($0);
    if (len&gt;4) {
      readlength[len]++;
    }
  }
}
END {
  for (i in readlength){
    countstored+=readlength[i];
    lensum+=readlength[i]*i;
    print i, readlength[i];
  }
  print "reads read = "countread &gt; "/dev/stderr";
  print "reads stored = "countstored &gt; "/dev/stderr";
  print "read average length = "lensum/countstored &gt; "/dev/stderr";
}


and I just wonder if it is possible to shorten it with bioawk?
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.