linux - 在 Linux 服务器上搜索最新版本的软件

标签 linux bash grep

好吧,我发现很难理解这个问题。有人可以帮我解决逻辑问题吗?

我有几台安装了各种软件的 Linux 服务器。我需要知道该软件的最新版本。

所以我 rpm -qa | grep 名称 |排序-n

并得到:

name_componentA_patch-9.10.9.1.0.0-3_es3.0
name_componentA_patch-9.10.9.2.0.5-2_es3.0
name_componentA_patch-9.10.9.3.2.1-2_es3.0
name_componentA_patch-9.10.9.4.2.0-1_es3.0
name_componentA_patch-9.10.9.5.0.2-1_es3.0

name_componentB-9.0.2.1.0-1_es3.0_es4.0
name_componentB-9.0.3.0.1-1_es3.0_es4.0
name_componentB-9.0.4.0.0-1_es3.0_es4.0

现在我需要将各种不同软件的最新版本输出到一个文件中。 这里的常量是“name”,其余变化直到“-”

我希望输出的文件是这样的:

name_componentA_patch-9.10.9.5.0.2-1_es3.0
name_componentB-9.0.4.0.0-1_es3.0_es4.0

所有软件依此类推。

最佳答案

假设版本已排序并且软件之间出现换行符(就像在您的示例中一样),您可以使用 sed 获取换行符之前的所有行:

blackbear@blackbear-notebook:~$ cat test
name_componentA_patch-9.10.9.1.0.0-3_es3.0
name_componentA_patch-9.10.9.2.0.5-2_es3.0
name_componentA_patch-9.10.9.3.2.1-2_es3.0
name_componentA_patch-9.10.9.4.2.0-1_es3.0
name_componentA_patch-9.10.9.5.0.2-1_es3.0

name_componentB-9.0.2.1.0-1_es3.0_es4.0
name_componentB-9.0.3.0.1-1_es3.0_es4.0
name_componentB-9.0.4.0.0-1_es3.0_es4.0

blackbear@blackbear-notebook:~$ sed -n '/^$/{g;1!p;};h' < test
name_componentA_patch-9.10.9.5.0.2-1_es3.0
name_componentB-9.0.4.0.0-1_es3.0_es4.0
blackbear@blackbear-notebook:~$ 

关于linux - 在 Linux 服务器上搜索最新版本的软件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20401251/

相关文章:

bash - 在 bash 中使用参数文本文件

linux - SSH 代理转发特定 key 而不是所有已注册的 ssh key

linux - 在 bash 脚本中 : using cat on a file with the contents ".*" yields unexpected results

php - 如何在后台运行具有参数的命令的命令

bash - pgrep -P,但对于孙子不仅仅是 child

linux - 测试文件中的每一行是否包含另一个文件中的多个字符串之一

linux - 在 Linux 中通过脚本请求用户输入并根据用户输入创建文件

regex - Linux - 想要检查可能的重复目录(可能需要 RegEx)

python - 如何使用 zdaemon 创建多个脚本守护进程?

php - Bash 和 PHP - 在一定时间后调用函数/命令