On 2021-04-15 23:47:34 +0200, Vincent Lefevre wrote: > I'm currently using version-sort in order to get integers sorted > in strings (due to the lack of simple numeric sort like in zsh), > but I've noticed some ugliness. This may be bugs, not I'm not sure > since the description of the sorting method in the Coreutils manual > takes several pages with all its exceptions, and I expect no-one > will try to understand. [...]
I use this mainly to sort files numerically with "ls -v", but in practice, this doesn't have the expected intent. Here's an example. First, -v seems useful in some simple case. Without -v: $ ls -1 /var/log/dpkg.log* /var/log/dpkg.log /var/log/dpkg.log.1 /var/log/dpkg.log.10.gz /var/log/dpkg.log.11.gz /var/log/dpkg.log.12.gz /var/log/dpkg.log.2.gz /var/log/dpkg.log.3.gz /var/log/dpkg.log.4.gz /var/log/dpkg.log.5.gz /var/log/dpkg.log.6.gz /var/log/dpkg.log.7.gz /var/log/dpkg.log.8.gz /var/log/dpkg.log.9.gz One can see that 10, 11, 12 come before 2, which is bad. The -v option solves this issue: $ ls -1v /var/log/dpkg.log* /var/log/dpkg.log /var/log/dpkg.log.1 /var/log/dpkg.log.2.gz /var/log/dpkg.log.3.gz /var/log/dpkg.log.4.gz /var/log/dpkg.log.5.gz /var/log/dpkg.log.6.gz /var/log/dpkg.log.7.gz /var/log/dpkg.log.8.gz /var/log/dpkg.log.9.gz /var/log/dpkg.log.10.gz /var/log/dpkg.log.11.gz /var/log/dpkg.log.12.gz But when one has several kinds of log files (different suffixes), the -v option makes the wanted order incorrect. Without -v, this is correct (well, here because the numbers remain less than 10): $ ls -1 /var/log/mail.* /var/log/mail.err /var/log/mail.err.1 /var/log/mail.err.2.gz /var/log/mail.err.3.gz /var/log/mail.err.4.gz /var/log/mail.log /var/log/mail.log.1 /var/log/mail.log.2.gz /var/log/mail.log.3.gz /var/log/mail.log.4.gz /var/log/mail.log.5.gz /var/log/mail.warn /var/log/mail.warn.1 /var/log/mail.warn.2.gz /var/log/mail.warn.3.gz /var/log/mail.warn.4.gz /var/log/mail.warn.5.gz But the -v option has an unwanted effect, where all log files without a number come first. $ ls -1v /var/log/mail.* /var/log/mail.err /var/log/mail.log /var/log/mail.warn /var/log/mail.err.1 /var/log/mail.err.2.gz /var/log/mail.err.3.gz /var/log/mail.err.4.gz /var/log/mail.log.1 /var/log/mail.log.2.gz /var/log/mail.log.3.gz /var/log/mail.log.4.gz /var/log/mail.log.5.gz /var/log/mail.warn.1 /var/log/mail.warn.2.gz /var/log/mail.warn.3.gz /var/log/mail.warn.4.gz /var/log/mail.warn.5.gz As a comparison, zsh with NUMERIC_GLOB_SORT gives the expected order in both cases: joooj% setopt NUMERIC_GLOB_SORT joooj% printf "%s\n" /var/log/dpkg.log* /var/log/dpkg.log /var/log/dpkg.log.1 /var/log/dpkg.log.2.gz /var/log/dpkg.log.3.gz /var/log/dpkg.log.4.gz /var/log/dpkg.log.5.gz /var/log/dpkg.log.6.gz /var/log/dpkg.log.7.gz /var/log/dpkg.log.8.gz /var/log/dpkg.log.9.gz /var/log/dpkg.log.10.gz /var/log/dpkg.log.11.gz /var/log/dpkg.log.12.gz joooj% printf "%s\n" /var/log/mail.* /var/log/mail.err /var/log/mail.err.1 /var/log/mail.err.2.gz /var/log/mail.err.3.gz /var/log/mail.err.4.gz /var/log/mail.log /var/log/mail.log.1 /var/log/mail.log.2.gz /var/log/mail.log.3.gz /var/log/mail.log.4.gz /var/log/mail.log.5.gz /var/log/mail.warn /var/log/mail.warn.1 /var/log/mail.warn.2.gz /var/log/mail.warn.3.gz /var/log/mail.warn.4.gz /var/log/mail.warn.5.gz -- Vincent Lefèvre <vinc...@vinc17.net> - Web: <https://www.vinc17.net/> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/> Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)