[clug] Sorting Version Numbers
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,
| Anyone have any thoughts on this?
Like you probably did, I did some searching on the web
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
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
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
with the addition of
sort -V orders as
where the last line should be the 2nd.
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 198 bytes
Desc: not available
More information about the linux