how to sort alphanumerically in Unix with sort? More complex than seems how to sort alphanumerically in Unix with sort? More complex than seems shell shell

how to sort alphanumerically in Unix with sort? More complex than seems


the -V option appears to do what you want - natural sorting. Intended for version numbers apparently (hence the letter chosen)

sort -V ~/headers

outputs

@42EBKAAXX090828:6:10:1077:1883/2@42EBKAAXX090828:6:100:1699:328/2@42EBKAAXX090828:6:102:785:808/2


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.