[clug] Sorting Version Numbers

Luke Mewburn lukem-clug at mewburn.net
Tue Jan 9 00:04:03 UTC 2018


On Tue, Jan 09, 2018 at 10:12:37AM +1100, jm via linux wrote:
  | Say you have a set of version numbers of the for x.y.z-m.n.o where
  | x,y,x, m, n, and o are positive integers, eg 2.6.32-541.5.12. Is there a
  | one liner that can sort these? I've tried piping it into sort but that
  | only seems to work with the first number (eg sort -n -t\- -k2). I could
  | solve it with a scripting language but it's part of a one liner to
  | remove old kernels from the system.
  | 
  | Here's an example of real input the I'm trying to sort,
  | 
  | kernel-2.6.32-642.11.1.el7.x86_64
  | kernel-2.6.32-642.1.1.el7.x86_64
  | kernel-2.6.32-573.22.1.el7.x86_64
  | kernel-2.6.32-642.4.2.el7.x86_64
  | kernel-2.6.32-642.13.1.el7.x86_64
  | 
  | Anyone have any thoughts on this?

Like you probably did, I did some searching on the web
and experimentation.

Your email prompted me to look into this again,
because I've had the same requirement...

My findings, in most-useful to least-useful order :)


1. Use yum tools that already exist: package-cleanup

I found
	package-cleanup --oldkernels --count=3
(in yum-utils on CentOS 6/7) seemed to do the right thing.
For one of my build systems where I have other kernel-devel packages
that I want to keep, I instead used:
	package-cleanup --oldkernels --keepdevel --count=3

See: https://www.centos.org/forums/viewtopic.php?t=2120



2. Use sort -V. (not quite there).

The "sort --version-compare" (AKA "sort -V") option in GNU
coreutils sort (which is on el7) almost does the right thing,
except that it doesn't list the "unpatched" kernel first.

E.g., given your input
	kernel-2.6.32-642.11.1.el7.x86_64
	kernel-2.6.32-642.1.1.el7.x86_64
	kernel-2.6.32-573.22.1.el7.x86_64
	kernel-2.6.32-642.4.2.el7.x86_64
	kernel-2.6.32-642.13.1.el7.x86_64
with the addition of
	kernel-2.6.32-642.el7.x86_64
sort -V orders as
	kernel-2.6.32-573.22.1.el7.x86_64
	kernel-2.6.32-642.1.1.el7.x86_64
	kernel-2.6.32-642.4.2.el7.x86_64
	kernel-2.6.32-642.11.1.el7.x86_64
	kernel-2.6.32-642.13.1.el7.x86_64
	kernel-2.6.32-642.el7.x86_64
where the last line should be the 2nd.

See: https://serverfault.com/questions/601427/sort-installed-packages-version


3. Implement your own python script importing rpmUtils (in yum)

Requires a bunch of mucking about with the guts of rpmUtils.
Probably not worth it.

See: http://blog.jasonantman.com/2014/07/how-yum-and-rpm-compare-versions/


cheers,
Luke.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/linux/attachments/20180109/34fc1e98/attachment.sig>


More information about the linux mailing list