Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for parsing package name, package version (including release) for Fedora / Red Hat packages

Tags:

regex

bash

sed

awk

My goal is to parse correctly package names and package versions (including release number) for Fedora or Red Hat packages like this:

python39-3.9.16-1.module+el8.8.0+18968+3d7b19f0.1.x86_64
python3.11-3.11.2-2.el9_2.2.aarch64
glibc-2.34-60.el9.aarch64
glib2-2.68.4-6.el9.aarch64
langpacks-core-font-en-3.0-16.el9.noarch
p11-kit-trust-0.24.1-2.el9.aarch64
tzdata-2023c-1.el9.noarch

Expected result:

pkg: python39   version: 3.9.16-1
pkg: python3.11 version: 3.11.2-2
pkg: glibc  version: 2.34-60
pkg: glib2  version: 2.68.4-6
pkg: langpacks-core-font-en version: 3.0-16
pkg: p11-kit-trust: version: 0.24.1-2
pkg: tzdata version: 2023c-1

Here is my attempt:

echo -e "python39-3.9.16-1.module+el8.8.0+18968+3d7b19f0.1.x86_64\npython3.11-3.11.2-2.el9_2.2.aarch64\nglibc-2.34-60.el9.aarch64\nglib2-2.68.4-6.el9.aarch64\nlangpacks-core-font-en-3.0-16.el9.noarch\np11-kit-trust-0.24.1-2.el9.aarch64\ntzdata-2023c-1.el9.noarch" > pkgs.txt
cat pkgs.txt | sed -E 's/([^-]*)-([0-9]+(\.[0-9]+)*(-[0-9]+)?)([^0-9].*)?/pkg: \1\tversion: \2/'

and i'm getting:

pkg: python39   version: 3.9.16-1
pkg: python3.11 version: 3.11.2-2
pkg: glibc  version: 2.34-60
pkg: glib2  version: 2.68.4-6
langpacks-core-font-pkg: en version: 3.0-16
p11-kit-pkg: trust  version: 0.24.1-2
pkg: tzdata version: 2023

Please help me to fix/improve a regular expression to parse correctly package names and package versions

UPDATE I'm going to execute this command in a container with minimum installed OS packages (similar to ubi-minimal). So I won't have perl and python installed, just a minimum set of commands like: cat, sed, awk, grep, etc.

like image 231
MaxU - stop WAR against UA Avatar asked Oct 29 '25 02:10

MaxU - stop WAR against UA


1 Answers

If you can use perl :

perl -pe 's/(.*?)-([0-9].*?)\.[^0-9].*/pkg: $1\tversion: $2/' << EOF
python39-3.9.16-1.module+el8.8.0+18968+3d7b19f0.1.x86_64
python3.11-3.11.2-2.el9_2.2.aarch64
glibc-2.34-60.el9.aarch64
glib2-2.68.4-6.el9.aarch64
langpacks-core-font-en-3.0-16.el9.noarch
p11-kit-trust-0.24.1-2.el9.aarch64
tzdata-2023c-1.el9.noarch
EOF

sed version, assuming there is no @ in package name :

sed -E 's/\.[^0-9].*$// # Remove everything after version
        s/(-[0-9].*)/@\1/ # Insert @ before version
        s/([^@]+)@-(.*)/pkg: \1\tversion: \2/' << EOF
python39-3.9.16-1.module+el8.8.0+18968+3d7b19f0.1.x86_64
python3.11-3.11.2-2.el9_2.2.aarch64
glibc-2.34-60.el9.aarch64
glib2-2.68.4-6.el9.aarch64
langpacks-core-font-en-3.0-16.el9.noarch
p11-kit-trust-0.24.1-2.el9.aarch64
tzdata-2023c-1.el9.noarch
EOF
like image 197
Philippe Avatar answered Oct 31 '25 17:10

Philippe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!