English 中文(简体)
Fedora / Red Hat 包裹名称登记表
原标题:RegEx for parsing package name, package version (including release) for Fedora / Red Hat packages

我的目标是,为Fedora或红外带包裹提供正确的包装名称和包装版本(包括释放号码):

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

预期结果:

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

我在此试图:

echo -e "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" > pkgs.txt
cat pkgs.txt | sed -E  s/([^-]*)-([0-9]+(.[0-9]+)*(-[0-9]+)?)([^0-9].*)?/pkg: 1	version: 2/ 

i m 正在:

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

请帮助我确定/改进定期表达,以正确划分包装名称和包装版本

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.

最佳回答

如果你能够使用,

perl -pe  s/(.*?)-([0-9].*?).[^0-9].*/pkg: $1	version: $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

www.un.org/Depts/DGACM/index_russian.htm

sed -E  s/.[^0-9].*$// # Remove everything after version
        s/(-[0-9].*)/@1/ # Insert @ before version
        s/([^@]+)@-(.*)/pkg: 1	version: 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
问题回答

利用RedHat或Fedora提供rpm的包裹管理人返还名称和版本,无疑比将Regex用于平价包装名称更可靠。

例:

#!/bin/sh
for pkg in
  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
do
  rpm -qp --queryformat  pkg: %{NAME} version: %{VERSION}  "$pkg.rpm"
done

Or if having all .rpm file names, a loop is not even required:

rpm -qp --queryformat  pkg: %{NAME} version: %{VERSION}  
  python39-3.9.16-1.module+el8.8.0+18968+3d7b19f0.1.x86_64.rpm 
  python3.11-3.11.2-2.el9_2.2.aarch64.rpm 
  glibc-2.34-60.el9.aarch64.rpm 
  glib2-2.68.4-6.el9.aarch64.rpm 
  langpacks-core-font-en-3.0-16.el9.noarch.rpm 
  p11-kit-trust-0.24.1-2.el9.aarch64.rpm 
  tzdata-2023c-1.el9.noarch.rpm

标有标有的样本 单线解决方案。 此处采用^(*?)-[0-9]+[a-zA-Z]*(?:[0-9]+*(?:-[a-zA-Z0-9]+)?),其中创建了2个捕捉小组,我们在按要求产出在这里进行替代时用于印刷价值。

perl -pe  s/^(.*?)-([0-9]+[a-zA-Z]*(?:.[0-9]+)*(?:-[a-zA-Z0-9]+)?)/pkg: $1	version: $2/   Input_file

如果您的集装箱有<代码>rpm 因此,指挥系统为你想要做的事情提供了最安全的途径(至少是另一个答案)。

如果没有<代码>rpm 那么,你可以根据,根据“RPM 文件所记载的组合命名标准,轻而安全地做你们想要做的事情。 格式是 姓名转换-release.architecture(NVRA)。 由于内部海标(dashes,- natures)只能在<代码><>/code>和architecture上出现。 包裹的名称从来都不包含一条 do子,而是直截了当地把包裹的名称放入其部分。

便携式(无<代码>-E>)sed 解决办法是:

sed  s/(.*)-(.*-[^.]*).*/pkg: 1 version: 2/  pkgs.txt

也可以通过标准操纵操纵进行。 https://www.shellcheck.net/“rel=“nofollow noreferer”>Shellcheck-clean Code with bash and dash :

#! /bin/sh -

while read -r nvra || [ -n "$nvra" ]; do
    nv=${nvra%-*}
    ra=${nvra##*-}
    n=${nv%-*}
    v=${nv##*-}
    printf  pkg: %s version: %s-%s
  "$n" "$v" "${ra%%.*}"
done <pkgs.txt

如果<编码>pkgs.txt文档(在1 000条线以上)庞大,那么纯壳代码就会明显缓慢。

Adding an awk answer here, since OP said perl may not be there in their container. Used GNU awk here.

  • Making field separator as (\+|\.)el[0-9]+(\.[0-9]*)* as per shown samples.
  • Then using match function to match regex -([0-9]+[a-zA-Z]*)([^-]+)(-[a-zA-Z0-9]+)? to get versions values here.
  • Then again using match function to get values before version, would have done it more easily if awk would have support lazy match :)
awk -F (\+|\.)el[0-9]+(\.[0-9]*)*   
match($1,/-([0-9]+[a-zA-Z]*)([^-]+)(-[a-zA-Z0-9]+)?/,arr){
   val=arr[1] arr[2] arr[3]
}
match($1,val){
   print "pkg: " substr($1,1,RSTART-2) "	version: " val
}
   Input_file




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签