shellsortingunixgnu

Linux command line utilities: Can sort(1) sort text by multiple (>2) columns?


I have the following data

foo-a12a11
foo-a12a2
foo-a12b12
foo-a12b21
foo-a13a10
foo-a13a1
foo-a13a9
IIII122344      # this indicates the order in which I want to consider the fields (I=ignore)

I want to sort it first by the single letter, then by the first number, then by the letter, then finally by the trailing number in numerical order.

From reading the man page of sort I thought this would be enough sort --key=1.1db,2.8n. But this returns sort: options '-dn' are incompatible ... which I find odd as I thought they should be treated as spearate keys. Trying to switch the second key to sort with d works, but sorts the trailing number in lexicographic order (i.e. 10 is placed before 9)

Anyone know how I achieve my desired sort (shown below, manually produced)?:

foo-a12a2
foo-a12a11
foo-a12b12
foo-a12b21
foo-a13a1
foo-a13a9
foo-a13a10

The foo-a is currently the same in all cases, so I've chosen to ignore it for purposes of sorting.


Solution

  • With fixed-width sections as shown (and assuming no blank characters), you could try:

    sort -k 1.5db,1.5 -k 1.6n,1.7 -k 1.8db,1.8 -k 1.9n