Here's one way to solve this problem:
- Add a second column, with the times converted to the same unit
- Sort the input on the second column
- Drop the second column
You could do step 1 with an awk script, this script converts the time units to nanoseconds:
{
time = $1;
sub(/[a-z]+$/, "", time);
unit = $1;
sub(/^[^a-z]+/, "", unit);
# convert to nanoseconds
if (unit == "us") {
time *= 1000;
} else if (unit == "ms") {
time *= 1000000;
} else if (unit == "s") {
time *= 1000000000;
}
print $1, time
}
If you save this in a file convert.awk
, then you can perform steps 1-3 with this pipeline:
awk -f convert.awk input.txt | sort -g -k2 | cut -f1 -d' '
The -g
instead of -n
is necessary in case some numbers get displayed in exponential notation. (Credit to OP for pointing that out.)
good question. to me this looks like 2 bugs in
sort -h
, or perhaps 1 bug and 1 un-fixable design limitation. 1)us
orµs
isn't properly handled (bug). 2)s
isn't properly handled by itself (possible design limitation). if you convert thes
to another unit that works properly (m
,k
,M
),sort -h
gets it right. (try it withmg/kg/Mg
values!) – quixotic – 2017-02-13T13:31:53.633