Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83; site mit-eddie.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxj!ihnp4!mit-eddie!smh
From: smh@mit-eddie.UUCP (Steven M. Haflich)
Newsgroups: net.unix
Subject: Re: Sort(1) on E-format numerics
Message-ID: <2794@mit-eddie.UUCP>
Date: Fri, 28-Sep-84 20:10:32 EDT
Article-I.D.: mit-eddi.2794
Posted: Fri Sep 28 20:10:32 1984
Date-Received: Sun, 30-Sep-84 02:30:22 EDT
References: <1181@hao.UUCP>
Distribution: net
Organization: MIT, Cambridge, MA
Lines: 38

Quoth woods@hao.UUCP (Greg "Bucket" Woods):
  We have a need to numerically sort files which contain columns of
  numbers in E-format, i.e. something of the form [+-]#.####e[+-]##, where
  "#" means a digit and [+-] means an optional sign.

God is the following solution UGLY!!!!!!  But it works...  As a test
case, I use the output of the following program.
	#include 
	main() { register int i; float foo = 0.0;
		for (i=90; i--; ) {	printf("foo %e bar\n", sin(foo));
					printf("foo %e bar\n", 123.*sin(foo));
					foo += .2;
		}
	}
The following a shell script will sort it on the E-format number in the
second whitespace-delimited field:
	( awk '$2 ~ /^-/ {
		{ n = split($2, number, "e") }
		{ if (number[2] ~ /^\+/) number[2] = " " substr(number[2],2) }
		{ print $1, number[1], number[2], $3 }
		}
	' $* | sort +2nr +1n; awk '$2 ~ /^[^-]/ {
		{ n = split($2, number, "e") }
		{ if (number[2] ~ /^\+/) number[2] = " " substr(number[2],2) }
		{ print $1, number[1], number[2], $3 }
		}
	' $* | sort +2n +1n) |
		awk '$3 ~ /^-/	{ print $1, $2 "e" $3, $4 }
	$3 ~ /^[^-]/	{ print $1, $2 "e+" $3, $4 }
	'
Sorry -- this crock demands real file(s) as input and won't read a
pipe.  Converting it to read the proper input field is left as an
exercise for the student.

What this proves to anyone still reading this gibberish is that shell
and awk scripts are easier to read than to write. :-) Have a nice day!

Steve Haflich, smh@mit-ems@mit-mc, {decvax!genrad, ihnp4}!mit-eddie!smh