how to sort alphanumerically in Unix with sort? More complex than seems
It is sorting it alphabetically as it is in your example. The reason 10:
is coming after 100
and 102
is that 10:
is after them, since the colon :
is after the 9
character in the ASCII chart.
If you're wanting to sort on the third field delimited by a colon, try this:
sort -t':' -k3 ~/headers > foo
This is usually called Natural Sorting. Here's one way that works for your example data set.
import redef natural_sorted(iterable, reverse=False): """Return a list sorted the way that humans expect.""" def convert(text): return int(text) if text.isdigit() else text def natural(item): return map(convert, re.split('([0-9]+)', item)) return sorted(iterable, key=natural, reverse=reverse)
I found this here and improved a bit.