141126Sbostic#!/bin/awk -f 241126Sbostic# 3*61890Sbostic# Copyright (c) 1990, 1993 4*61890Sbostic# The Regents of the University of California. All rights reserved. 541126Sbostic# 641126Sbostic# This code is derived from software contributed to Berkeley by 741126Sbostic# Van Jacobson. 841126Sbostic# 941126Sbostic# %sccs.include.redist.sh% 1041126Sbostic# 11*61890Sbostic# @(#)median.awk 8.1 (Berkeley) 06/06/93 1241126Sbostic# 1341123Sbostic/^ *[0-9]/ { 1441123Sbostic # print out the median time to each hop along a route. 1541123Sbostic tottime = 0; n = 0; 1641123Sbostic for (f = 5; f <= NF; ++f) { 1741123Sbostic if ($f == "ms") { 1841123Sbostic ++n 1941123Sbostic time[n] = $(f - 1) 2041123Sbostic } 2141123Sbostic } 2241123Sbostic if (n > 0) { 2341123Sbostic # insertion sort the times to find the median 2441123Sbostic for (i = 2; i <= n; ++i) { 2541123Sbostic v = time[i]; j = i - 1; 2641123Sbostic while (time[j] > v) { 2741123Sbostic time[j+1] = time[j]; 2841123Sbostic j = j - 1; 2941123Sbostic if (j < 0) 3041123Sbostic break; 3141123Sbostic } 3241123Sbostic time[j+1] = v; 3341123Sbostic } 3441123Sbostic if (n > 1 && (n % 2) == 0) 3541123Sbostic median = (time[n/2] + time[(n/2) + 1]) / 2 3641123Sbostic else 3741123Sbostic median = time[(n+1)/2] 3841123Sbostic 3941123Sbostic print $1, median 4041123Sbostic } 4141123Sbostic} 42