shellsortingcommand-lineprefix

In bash, how to sort while ignoring a prefix?


I know the sort command can be made to use only some of the information on each input line as the sort key, e.g. using sort -k 2 to skip the initial sequence of characters up to a whitespace; and I know I can use a different field delimiter with -t.

Suppose, though, that I want to sort input while ignoring the first few characters - which are not fixed. For example, this:

bar10
baz12
foo1
gaa11
quu2

should become, using only the 4th character and onwards:

foo1
bar10
gaa11
baz12
quu2

Of course I can do this with a more involved scripting language, like awk (e.g. this way). But is there something simpler I could do instead?


Solution

  • As mentioned in the comment, it's just: sort -k1.4


    The syntax understood by sort is rather drily explained in the standard: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html

    (But it does provide a few examples.)

    It's worth remembering that locale settings affect output ordering.