Вычисление среднего и медианы с помощью awk (gawk)

Код awk:

awk '{i[NR] = $1; sum += i[NR] } END \
     {asort(i); if (NR > 0) { if (NR%2 == 0) {m = (i[NR/2]+i[NR/2+1])/2} ; \
     if (NR%2 == 1) {m = (i[(NR+1)/2])} \
     printf "Count: %s\nMin: %s\nMax: %s\nMed: %s\nAvg: %s\n", NR, i[1], i[NR], m, sum / NR }}'

Пример:

~ for i in 5 5000 5 5 5 5 5 100005; do echo ${i}; done |\
  awk '{i[NR] = $1; sum += i[NR] } END {asort(i); \
  if (NR > 0) { if (NR%2 == 0) {m = (i[NR/2]+i[NR/2+1])/2} ; \
  if (NR%2 == 1) {m = (i[(NR+1)/2])} \
  printf "Count: %s\nMin: %s\nMax: %s\nMed: %s\nAvg: %s\n", NR, i[1], i[NR], m, sum / NR }}'

  Count: 8
  Min: 5
  Max: 100005
  Med: 5
  Avg: 13129.4
Яндекс.Метрика