I need to sort list of fetched artifact names from Artifactory and pick the one with the highest version.
The problem is that some of the artifact folders contains also artifact with incorrect naming *-SNAPSHOT.jar
I have no problem to pick up the highest version from the list where the *-SNAPSHOT.jar is missing but I have problem in other cases. I need to do that using one script regardless of whether there is badly named artifact or not.
My current input is e.g. this one:
Some-Component-4.9.1-20220617.072611-5.jar
Some-Component-4.9.1-20220617.072611-5.pom
Some-Component-4.9.1-20220617.072611-4.jar
Some-Component-4.9.1-20220617.072611-4.pom
Some-Component-4.9.1-20220617.072611-3.jar
Some-Component-4.9.1-20220617.072611-3.pom
Some-Component-4.9.1-20220617.072611-2.jar
Some-Component-4.9.1-20220617.072611-2.pom
Some-Component-4.9.1-SNAPSHOT.jar
Until now I just used sort -V filename.txt | tail -1
to obtain the line with the highest version. But now there is still the SNAPSHOT version bothering me and I can't figure out how to solve this problem and possibly find solution that will work for both scenarios - list with or without the SNAPSHOT version.
CodePudding user response:
What about:
cat filename.txt | grep -v SNAPSHOT | sort -V | tail -1
The grep command will output any line from input that does not contain SNAPSHOT
CodePudding user response:
Just remove the line containing SNAPSHOT
sort -V filename.txt | sed -E "/SNAPSHOT/d" | tail -1