BSD does not provide -V
by default, so Ben's solution is as close as it gets. For your convenience I post here our version that is able to sort files like <label>-<version>.<ext>
:
% ls bla-*.ime | sed -Ee 's/^(.*-)([0-9.]+)(\.ime)$/\2.-1 \1\2\3/' | sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 | cut -d\ -f2-
bla-1.ime
bla-1.0.ime
bla-1.0.0.ime
bla-1.1.ime
bla-1.1.29.ime
bla-1.2.3.ime
bla-1.2.29.ime
bla-1.2.30.ime
bla-1.3.ime
bla-1.3.0.ime
bla-1.3.1.ime
bla-1.3.10.ime
bla-1.3.20.ime
bla-1.7.ime
bla-1.11.29.ime
bla-2.3.2.ime
bla-11.2.2.ime
Short explanation:
- List the files that you want to sort with
ls
.
- Find the version number and prefix the line with that.
- While doing that add
-1
to the end to make shorter version number sort first (before .0
even). You could change -1
to 0
if you consider 1.3
to be equivalent to 1.3.0
.
- Sort the lines using Ben's suggested solution on the version number.
- Chop off the version prefix from the line.
The list now contains a version sorted list of applicable file names. Any additional sorting on the label
part is left as an exercise to the reader.