Sunday 12 June 2016

Shell script trick to sort release versions

Unix sort is not designed to sort release versions numerically.
The versions will look like:  v1.25.3 or v1.3.1.
Sort will think that 1.3 is bigger than 1.25 numerically, and thus will not sort the versions as expected.
This became an issue in a script i was creating to detect the latest snapshot version on an apache archiva repo to support release work.

To work around this, i have replace the "." with "," before i did the sort and then replaced the "." back.
This will make sort think we are looking at different numbers and the "." will not interfere, thus will look at the versions as: v1,25,3 and v1,3,1 and thus will think that 25 is bigger than 3 as we expect.

Below is the script:

cat get_snapshots.sh
for SERVICE in `grep fileSourceURL archiva_URL_list |sort |cut -d":" -f3,6-|cut -d"/" -f1-9|tr -d "'"|tr -d " "|sed -e "s/$/\//g"`
do
    serv_name=`echo ${SERVICE}|cut -d":" -f1`
    url=`echo ${SERVICE}|cut -d":" -f2-`
    SNAPSHOT=`curl ${url} 2>/dev/null|grep '<li>'|egrep -v "xml|proj|maven|2016|2015"|cut -d">" -f3-|cut -d"<" -f1|tr "." ","|sort -n -t"," -k1,2|tr "," "."|tail  -1`
    echo ${serv_name}:${SNAPSHOT}
done


The curl command is piped to an egrep that would remove all the unwanted HTML tags and other maven generated files part of the build.

As can be seen the sort is between 2 tr command, first will replace the "." with a "," and the 2nd will revert the "." back.
Sort uses the "," as separator and it does the sort on the first field then on the 2nd.
I am assuming minor versions are not updated as often but this script can still be extended further to even longer version numbers.

Could be useful some day !!

No comments:

Post a Comment