Skip to main content
4 of 5
added 177 characters in body; added 64 characters in body; added 14 characters in body; deleted 1 characters in body
Peter.O
  • 33.8k
  • 32
  • 120
  • 167

Just for the sake of having a variety of options presented, I've put together this "bash + single-purpose tools" method.

I like bash, but for this particular purpose I'll probably use Rscript; it is just so terse, and it will be handy for other more involved mathematical jobs...

For bash to handle floating-point numbers, this script uses numprocess and numaverage from package num-utils.

PS. I've also had a reasonable look at bc, but for this particular job, it doesn't offer anything beyond what awk does. It is (as the 'c' in 'bc' states) a calculator—a calculator which requires a much programming as awk and this bash script...


arr=($(sort -n "LIST" |tee >(numaverage 2>/dev/null >stats.avg) )) cnt=${#arr[@]}; ((cnt==0)) && { echo -e "0\t0\t0\t0\t0"; exit; } mid=$((cnt/2)); if [[ ${cnt#${cnt%?}} == [02468] ]] then med=$( echo -n "${arr[mid-1]}" |numprocess /+${arr[mid]},%2/ ) else med=${arr[mid]}; fi # count min max median average echo -ne "$cnt\t${arr[0]}\t${arr[cnt-1]}\t$med\t"; cat stats.avg 
Peter.O
  • 33.8k
  • 32
  • 120
  • 167