4

I wrote a script for a Linux bash shell.

One line takes a list of filenames and sorts them. The list looks like this:

char32.png char33.png [...] char127.png

It goes from 32 to 127. The default sorting of ls of this list is like this

char100.png char101.png [...] char32.png char33.png [...] char99.png

Luckily, there is sort, which has the handy -V switch which sorts the list correctly (as in the first example).

Now, I have to port this script to OSX and sort in OSX is lacking the -V switch.

Do you have a clever idea of how to sort this list correctly?

bastibe
  • 16,551
  • 28
  • 95
  • 126

1 Answers1

5

Do they all start with a fixed string (char in your example)? If so:

sort -k1.5 -n

-k1.5 means to sort on the first key (there’s only one key in your example) starting from the 5th character, which will be the first digit. -n means to sort numerically. This works on Linux too.

Nate
  • 18,752
  • 8
  • 48
  • 54