使用 Bash 从包名称中去除版本

发布于 2024-09-03 03:26:52 字数 441 浏览 7 评论 0原文

我正在尝试仅使用 Bash 从包名称中删除版本。我有一个解决方案,但我认为这不是最好的解决方案,所以我想知道是否有更好的方法。我所说的更好是指更干净、更容易理解。

假设我有字符串“my-program-1.0”,而我只想要“my-program”。我当前的解决方案是:

#!/bin/bash

PROGRAM_FULL="my-program-1.0"
INDEX_OF_LAST_CHARACTER=`awk '{print match($0, "[A-Za-z0-9]-[0-9]")} <<< $PROGRAM_FULL`
PROGRAM_NAME=`cut -c -$INDEX_OF_LAST_CHARACTER <<< $PROGRAM_FULL`

实际上,“包名称”语法是一个 RPM 文件名(如果有的话)。

谢谢!

I'm trying to strip the version out of a package name using only Bash. I have one solution but I don't think that's the best one available, so I'd like to know if there's a better way to do it. by better I mean cleaner, easier to understand.

suppose I have the string "my-program-1.0" and I want only "my-program". my current solution is:

#!/bin/bash

PROGRAM_FULL="my-program-1.0"
INDEX_OF_LAST_CHARACTER=`awk '{print match($0, "[A-Za-z0-9]-[0-9]")} <<< $PROGRAM_FULL`
PROGRAM_NAME=`cut -c -$INDEX_OF_LAST_CHARACTER <<< $PROGRAM_FULL`

actually, the "package name" syntax is an RPM file name, if it matters.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞别 2024-09-10 03:26:52

非常适合 sed:

# Using your matching criterion (first hyphen with a number after it
PROGRAM_NAME=$(echo "$PROGRAM_FULL" | sed 's/-[0-9].*//')

# Using a stronger match
PROGRAM_NAME=$(echo "$PROGRAM_FULL" | sed 's/-[0-9]\+\(\.[0-9]\+\)*$//')

第二个匹配确保版本号是由点分隔的数字序列(例如 X、XX、XXX,...)。

编辑:因此,基于版本号概念定义不明确的事实,到处都有评论。您必须为您期望的输入编写正则表达式。希望您不会遇到像“program-name-1.2.3-a”那样可怕的事情。不过,如果没有OP的任何额外要求,我认为这里的所有答案都足够好了。

Pretty well-suited to sed:

# Using your matching criterion (first hyphen with a number after it
PROGRAM_NAME=$(echo "$PROGRAM_FULL" | sed 's/-[0-9].*//')

# Using a stronger match
PROGRAM_NAME=$(echo "$PROGRAM_FULL" | sed 's/-[0-9]\+\(\.[0-9]\+\)*$//')

The second match ensures that the version number is a sequence of numbers separated by dots (e.g. X, X.X, X.X.X, ...).

Edit: So there are comments all over based on the fact that the notion of version number isn't very well-defined. You'll have to write a regex for the input you expect. Hopefully you won't have anything as awful as "program-name-1.2.3-a". Absent any additional request from the OP though, I think all the answers here are good enough.

衣神在巴黎 2024-09-10 03:26:52

Bash:

program_full="my-program-1.0"
program_name=${program_full%-*}    # remove the last hyphen and everything after

生成“my-program”

program_full="alsa-lib-1.0.17-1.el5.i386.rpm"
program_name=${program_full%%-[0-9]*}    # remove the first hyphen followed by a digit and everything after

​​生成“alsa-lib”

Bash:

program_full="my-program-1.0"
program_name=${program_full%-*}    # remove the last hyphen and everything after

Produces "my-program"

Or

program_full="alsa-lib-1.0.17-1.el5.i386.rpm"
program_name=${program_full%%-[0-9]*}    # remove the first hyphen followed by a digit and everything after

Produces "alsa-lib"

·深蓝 2024-09-10 03:26:52

怎么样:

$ echo my-program-1.0 | perl -pne 's/-[0-9]+(\.[0-9]+)+$//'
my-program

How about:

$ echo my-program-1.0 | perl -pne 's/-[0-9]+(\.[0-9]+)+$//'
my-program
若能看破又如何 2024-09-10 03:26:52
echo pkg_name | sed 's/-[^-]*-[^-]*\.[^.]*\.[^.]*$//'

当传递完整的包名称来代替 pkg_name 时,它会删除版本并仅提供包名称。

echo pkg_name | sed 's/-[^-]*-[^-]*\.[^.]*\.[^.]*$//'

When the full package name is passed in place of pkg_name, it removes the version and gives only the package name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文