Thursday, September 27, 2007

Shell script alphanumeric comparison

Ever needed to compare Alphanumeric values say :

r1v07l09ab < r1v07l09ac

Well Shell script and awk are your companions as always. Below is one way of doing it.

prevRelease=r1v07l09ab
currRelease=r1v07l09ac
VALID=`echo "$prevRelease $currRelease" | awk '{ if($1 < $2) print "1"; else print "0" }'`



The above code also gives you another way of sending Shell variable to Awk and to return the response of the alphanumeric comparison back to the Shell.