1

This is similiar to this question: remove the low version number of file

Unfortunately the solution to the above question does not work for me, example:

Files (ls -vr *.pkg.tar.xz):

wire-desktop-bin-2.11.2700-1-x86_64.pkg.tar.xz
wire-desktop-bin-2.11.2698-1-x86_64.pkg.tar.xz
wire-desktop-bin-2.11.2697-1-x86_64.pkg.tar.xz
webstorm-2016.3.3-1-x86_64.pkg.tar.xz
webstorm-2016.3.2-1-x86_64.pkg.tar.xz
visual-studio-code-1.9.0-1-x86_64.pkg.tar.xz
visual-studio-code-1.8.1-3-x86_64.pkg.tar.xz
pycharm-professional-2016.3.2-1-any.pkg.tar.xz
plasma5-applets-thermal-monitor-git-r63.fd41970-1-any.pkg.tar.xz
masterpdfeditor-qt5-3.7.10-2-x86_64.pkg.tar.xz
masterpdfeditor-4.0.30-1-x86_64.pkg.tar.xz
keepass-plugin-keeagent-0.8.1-3-any.pkg.tar.xz
jre-8u121-1-x86_64.pkg.tar.xz
jdk-8u121-1-x86_64.pkg.tar.xz
jabref-3.8.1-1-any.pkg.tar.xz
intellij-jdk-8u112b719-1-x86_64.pkg.tar.xz
intellij-jdk-8u112b657-1-x86_64.pkg.tar.xz
intellij-idea-ultimate-edition-2016.3.4-1-any.pkg.tar.xz
intellij-idea-ultimate-edition-2016.3.3-1-any.pkg.tar.xz
hstr-git-1.21.r10.gc0d3236-1-x86_64.pkg.tar.xz
google-chrome-56.0.2924.87-1-x86_64.pkg.tar.xz
google-chrome-56.0.2924.76-1-x86_64.pkg.tar.xz
gitinspector-0.4.4-1-any.pkg.tar.xz
dropbox-17.4.33-1-x86_64.pkg.tar.xz
chromium-widevine-1:1.4.8.962-1-x86_64.pkg.tar.xz

Now I need to find (and later remove) all old packages (version number != highest).

Using the solution from the other question:

ls -vr *.depot | awk -F- '$1 == name{system ("rm \""$0"\"")}{name=$1}'

produces this wrong result:

:: os/x86_64 » ls -vr *.pkg.tar.xz | awk -F- '$1 == name{system ("echo \""$0"\"")}{name=$1}'
wire-desktop-bin-2.11.2698-1-x86_64.pkg.tar.xz
wire-desktop-bin-2.11.2697-1-x86_64.pkg.tar.xz
webstorm-2016.3.2-1-x86_64.pkg.tar.xz
visual-studio-code-1.8.1-3-x86_64.pkg.tar.xz
masterpdfeditor-4.0.30-1-x86_64.pkg.tar.xz
intellij-jdk-8u112b657-1-x86_64.pkg.tar.xz
intellij-idea-ultimate-edition-2016.3.4-1-any.pkg.tar.xz <-----------
intellij-idea-ultimate-edition-2016.3.3-1-any.pkg.tar.xz <-----------
google-chrome-56.0.2924.76-1-x86_64.pkg.tar.xz

Notice this lists both idea-ultimate-edition packages because of intellij-jdk (awk splits at first -). I don't really know how to solve this problem consistently.

These are Archlinux packages, even if pacman (Archlinux package manger) could help me here, I am building and storing them on a Debian system, so I would prefer a pure bash/awk/etc. solution.

2
  • 1
    This is pretty hard to get right, since version "designers" can be unusually creative. :) One way to get the name with awk that mostly works for your list above is name = $0; sub("-[0-9].*", "", name), i.e. look for the first dash followed by a digit. This fails for plasma5-applets-thermal-monitor-git-r63.fd41970-1-any.pkg.tar.xz though. It would be a lot safer if you could convince your package manager to give you the names and versions separately. Commented Feb 2, 2017 at 21:55
  • Thanks, yeah it doesn't seem to be trivial .. I your suggestion is good though, I maybe can check for dash followed by "\w?\d.*", I might play around with different regular expressions if there is no really better solution.
    – dav1d
    Commented Feb 3, 2017 at 12:13

1 Answer 1

1

You need a compley regex to isolate the moving version string. Delimited the basename with a unused character and sort it unique.

For example, build a index file with = as delimter:

perl -le 'map { print "$1=$2" if /(\S+)-((\S+-\d+)-(x86_64|any)\.\S+)/ } @ARGV' *.pkg.tar.xz > pkgindex

Sort it unique and change the delimiter back:

sort -t= -r -k1,1 -u pkgindex | sed s/=/-/ > freshpkgs

Don't forget to fill the architecture string with all known values (x86_64, i386, any)!

1
  • Thanks, that is the best I have so far, I don't like that I have to manually tweak the regex every time something changes, but I guess the actual problem isn't so trivial!
    – dav1d
    Commented Feb 5, 2017 at 12:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .