Sort(1) on E-format numerics
Steven M. Haflich
smh at mit-eddie.UUCP
Sat Sep 29 10:10:32 AEST 1984
Quoth woods at 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 <math.h>
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 at mit-ems@mit-mc, {decvax!genrad, ihnp4}!mit-eddie!smh
More information about the Comp.unix
mailing list