我想使用sed或awk命令仅打印列表中的行,这些行在模式1方面是唯一的,并且具有模式2的最高值。输入是文件名列表,是特定模型的软件版本。型号和软件版本都包含在文件名中。模式如此:
a,b_x.y.z_
或a,b_x.y_
模式1是a,b
(模型)
模式2是x.y.z
(软件版本)
对于a,b
的每个唯一版本,我想找到最新版本的x.y.z
或x.y
N.B。我不想按创建或修改的时间等进行搜索...这必须通过软件版本和型号的字符串来完成
答案 0 :(得分:1)
就像@shellter所说sed
真的不适合这个。我会使用awk
或类似的东西。对于版本号,每个子编号都需要在数字上进行比较。你可以尝试这样的事情:
awk '
BEGIN{
FS=OFS="_"
}
{
# Use "." to split current and version numbers into field arrays
m=split($2,New,/\./)
n=split(Version[$1],Current,/\./)
# loop from 1 through the highest number of fields (whichever of the two versions contains the most fields)
for(i=1; i<=(m>n?m:n); i++) {
# they are unequal no need to compare further fields, if the new one is higher then replace.
if(New[i]!=Current[i]){
if(New[i]>Current[i]) Version[$1]=$2
next
}
}
}
END{
for(i in Version)print i,Version[i]
}
' file
我做了一些修改来打印最后一部分并忽略比较中的目录树。看看是否有效:
awk -F_ '
{
# save current record
p=$0
# remove directory info
sub(/.*\//,x)
# Use "." to split current and version numbers into array
m=split($2,New,/\./)
n=split(Version[$1],Current,/\./)
# loop from 1 through the highest number of fields (whichever of the two versions contains the most fields)
for(i=1; i<=(m>n?m:n); i++) {
# they are unequal no need to compare further fields, if the new one is higher then replace.
if(New[i]!=Current[i]) {
if(New[i]>Current[i]) {
Version[$1]=$2
Line[$1]=p
}
next
}
}
}
END{
for(i in Version)print Line[i]
}
' file