1*41126Sbostic#!/bin/awk -f
2*41126Sbostic#
3*41126Sbostic# Copyright (c) 1990 The Regents of the University of California.
4*41126Sbostic# All rights reserved.
5*41126Sbostic#
6*41126Sbostic# This code is derived from software contributed to Berkeley by
7*41126Sbostic# Van Jacobson.
8*41126Sbostic#
9*41126Sbostic# %sccs.include.redist.sh%
10*41126Sbostic#
11*41126Sbostic#	@(#)median.awk	5.2 (Berkeley) 04/28/90
12*41126Sbostic#
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