views:

76

answers:

1

I'm working on a some build scripts that I'd like to depend on only standardized features. I need to sort some files by version. Say the files are bar-1.{0,2,3} bar-11.{0,2,3}.

By default, ls gives me:

bar-1_0
bar-11_0
bar-11_2
bar-11_3
bar-1_2
bar-1_3

Getting what I want is easy using 'ls -v':

bar-1_0
bar-1_2
bar-1_3
bar-11_0
bar-11_2
bar-11_3

The problem is that 'ls -v' is not standard. Standard sort also seems to lack the option I want, though I could be looking at old versions of the specs.

Can anyone suggest a portable way to achieve this effect short of writing my own sort routine?

Thanks, Rhys

A: 

sort -n -t- -k2 seems to do what you want. -n gives you numeric (i.e. not alphabetic) sort; -t- sets the field separator to -, and -k2 selects the second field, i.e. the version number.

My sort, on Ubuntu, even does the part with the underscore correctly, but I'm not sure if that is standard. To be sure, you could sort by the minor version first, then by major version.

Thomas
Thank you very much.
Rhys Ulerich