How to sort mixed text&numbers (eg hostnames)

sort

Given input with mixed text and numbers (w/o leading zeros), how can I get it sorted in the "natural" order? For example, given the following input (hostnames):

whatever-1.example.org
whatever-10.example.org
whatever-11.example.org
whatever-12.example.org
whatever-13.example.org
whatever-2.example.org
whatever-3.example.org
whatever-4.example.org
whatever-5.example.org
whatever-6.example.org
whatever-7.example.org
whatever-8.example.org
whatever-9.example.org

I would like this output:

whatever-1.example.org
whatever-2.example.org
whatever-3.example.org
whatever-4.example.org
whatever-5.example.org
whatever-6.example.org
whatever-7.example.org
whatever-8.example.org
whatever-9.example.org
whatever-10.example.org
whatever-11.example.org
whatever-12.example.org
whatever-13.example.org

EDIT

I should have mentioned that in addition to the "whatever"s there would also be

thingaroo-#.example.org
      .
      :

blargh-#.example.org
      .
      :

...etc...

Thanks!

Best Answer

If you have GNU coreutils ≥ 7.0, then you can use version sort. This is lexicographic order except that sequences of digits are ordered according to their value as an integer in decimal notation.

sort -V
Related Question