输入:
ganglia-3.7.2-2.el7.x86_64.rpm
ganglia-debuginfo-3.7.2-2.el7.x86_64.rpm
ganglia-devel-3.7.2-2.el7.x86_64.rpm
ganglia-gmetad-3.7.2-2.el7.x86_64.rpm
ganglia-gmond-3.7.2-2.el7.x86_64.rpm
ganglia-gmond-python-3.7.2-2.el7.x86_64.rpm
ganglia-web-3.7.1-2.el7.x86_64.rpm
ganglia-3.8.2-2.el7.x86_64.rpm
ganglia-debuginfo-3.8.2-2.el7.x86_64.rpm
ganglia-devel-3.8.2-2.el7.x86_64.rpm
ganglia-gmetad-3.8.2-2.el7.x86_64.rpm
ganglia-gmond-3.8.2-2.el7.x86_64.rpm
ganglia-gmond-python-3.8.2-2.el7.x86_64.rpm
ganglia-web-3.8.1-2.el7.x86_64.rpm
输出应该是:
ganglia-3.8.2-2.el7.x86_64.rpm
ganglia-debuginfo-3.8.2-2.el7.x86_64.rpm
ganglia-devel-3.8.2-2.el7.x86_64.rpm
ganglia-gmetad-3.8.2-2.el7.x86_64.rpm
ganglia-gmond-3.8.2-2.el7.x86_64.rpm
ganglia-gmond-python-3.8.2-2.el7.x86_64.rpm
ganglia-web-3.8.1-2.el7.x86_64.rpm
我经历了一种方法,比如采用前缀并使用ls -v
命令来获取版本......
ganglia
ganglia-debuginfo
ganglia-dlevel
ganglia-web
.....
并使用ls -v prefix* | tail -n 1 ..
我可以获得完美的东西,但仅限于神经节前缀。我无法得到完美的东西。
答案1
$ sed -E 's/([a-z])-([[:digit:]]+\.)+/\1 \2/' file |
sort -k2Vr |
awk '$1 in a{ next } { a[$1]; print $1 "-" $2 }'
ganglia-8.2-2.el7.x86_64.rpm
ganglia-debuginfo-8.2-2.el7.x86_64.rpm
ganglia-devel-8.2-2.el7.x86_64.rpm
ganglia-gmetad-8.2-2.el7.x86_64.rpm
ganglia-gmond-8.2-2.el7.x86_64.rpm
ganglia-gmond-python-8.2-2.el7.x86_64.rpm
ganglia-web-8.1-2.el7.x86_64.rpm
sed
-
:将名称和版本后缀之间的第一个替换为空格字符sort
:在第二个字段中按版本按相反顺序排列awk
:如果数组中存在名称,则跳过该行,否则将名称保存在数组中并打印名称-
和版本后缀
答案2
下面可能有助于按包的主要版本和次要版本进行排序,只是一个想法可能会有所帮助。
$ cat test | sed -e 's/-\([[:alpha:]]\)/+\1/g' | sort -t - -n -k2 -r | sed -e 's/+\([[:alpha:]]\)/-\1/g'
ganglia-web-3.8.1-2.el7.x86_64.rpm
ganglia-gmond-python-3.8.2-2.el7.x86_64.rpm
ganglia-gmond-3.8.2-2.el7.x86_64.rpm
ganglia-gmetad-3.8.2-2.el7.x86_64.rpm
ganglia-devel-3.8.2-2.el7.x86_64.rpm
ganglia-debuginfo-3.8.2-2.el7.x86_64.rpm
ganglia-3.8.2-2.el7.x86_64.rpm
ganglia-web-3.7.1-2.el7.x86_64.rpm
ganglia-gmond-python-3.7.2-2.el7.x86_64.rpm
ganglia-gmond-3.7.2-2.el7.x86_64.rpm
ganglia-gmetad-3.7.2-2.el7.x86_64.rpm
ganglia-devel-3.7.2-2.el7.x86_64.rpm
ganglia-debuginfo-3.7.2-2.el7.x86_64.rpm
ganglia-3.7.2-2.el7.x86_64.rpm
答案3
和zsh
:
typeset -A max_version
for file (*.rpm(nN)) max_version[${file%-*-*.*.*.*}]=$file
print -rC1 -- $max_version
其中当前目录中的文件*.rpm(nN)
列表按数字排序。我们将包(in ).rpm
的最大版本记录在关联数组中。x
x-version-packageVersion.rel.arch.rpm
max_version
该关联数组最终包含以下内容:
$ typeset -p max_version typeset -A max_version=( [ganglia]=ganglia-3.8.2-2.el7.x86_64.rpm [ganglia-debuginfo]=ganglia-debuginfo-3.8.2-2.el7.x86_64.rpm [ganglia-devel]=ganglia-devel-3.8.2-2.el7.x86_64.rpm [ganglia-gmetad]=ganglia-gmetad-3.8.2-2.el7.x86_64.rpm [ganglia-gmond]=ganglia-gmond-3.8.2-2.el7.x86_64.rpm [ganglia-gmond-python]=ganglia-gmond-python-3.8.2-2.el7.x86_64.rpm [ganglia-web]=ganglia-web-3.8.1-2.el7.x86_64.rpm )