Sorting Version Numbers

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Sorting Version Numbers

Samba - linux mailing list
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?


Jeff.




--
linux mailing list
[hidden email]
https://lists.samba.org/mailman/listinfo/linux
Reply | Threaded
Open this post in threaded view
|

Re: Sorting Version Numbers

Samba - linux mailing list
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.

--
linux mailing list
[hidden email]
https://lists.samba.org/mailman/listinfo/linux

attachment0 (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Sorting Version Numbers

Samba - linux mailing list
On 9/01/2018 11:04, Luke Mewburn wrote:

>   | 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

Didn't know this one about this one which I guess isĀ  I asked. From that
web page it seems adding installonly_limit=3 to /etc/yum.conf may remove
my need for this step all together though some of the reports also state
that it doesn't work.

It still seems I need to run,

grub2-mkconfig -o /boot/grub2/grub.cfg

which isn't a problem. I've used these two steps on a number of machines
today and it's been working fine.

>
> 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
Came across this after posting the email. Read the man page (
https://www.gnu.org/software/coreutils/manual/html_node/Details-about-version-sort.html
) and didn't realise that the caveats would bite me. My test case isn't
as complete as it should be. You've save me from potentially deleting a
needed kernel sometime in the future.

> 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/
>
Yeah, I'm trying to avoid that at all cost. It just introduces yet more
stuff I have to maintain.

Base on what you've told me number 1 is the the way to go.

Jeff.

--
linux mailing list
[hidden email]
https://lists.samba.org/mailman/listinfo/linux