04-19-2012
How to sort version numbers?
I would like to know how to sort version numbers, using bash or perl. I would like to sort file names that are program names with version numbers and extensions, such as hello-0.2.3.tar.gz and hello-0.10.3.tar.gz.
Version numbers of computer programs do not comply with the mathematical rule concerning the below-decimal-point part. Mathematics treats the part below decimal point differently from the integral part. In mathematics, the following sort order holds.
0.1 = 0.10 = 0.100 < 0.11 < 0.2 < 0.3
On the other hand, the version numbering system treats the part below decimal point in the same manner as the integral part. In the version numbering system, the following sort order holds.
0.1.0 < 0.2.0 < 0.3.0 < 0.10.0 < 0.11.0 < 0.100.0
The "sort" command on bash fails to achieve this latter result. How can one achieve this latter result?
Many thanks in advance.
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Hi There!
Apologies if this has been asked previously but I couldn't find the answer I was hoping for.
Basically, all I want to do is compare the OS X version against the version that I require in my script. So I'm retrieving the OS version using defaults read, but how can I compare this... (10 Replies)
Discussion started by: davewg
10 Replies
2. Shell Programming and Scripting
Hi,
I have a need to rename a list of files with a suffix .v1, .v2, v3 etc. and would like to keep a maximum of 6 versions.
example:
I have abc.sql in my current dir. and as part of a successful completion of another script I want to move abc.sql -> abc.sql.v1.
When I run the original... (3 Replies)
Discussion started by: try2shell
3 Replies
3. Shell Programming and Scripting
Hey guys, I have a file that contains the following:
366 K
364 Q
12 UB
7 INC. P
4 Law
2 LAMB
2 High
1 QEG
1 OF
1 LC
1 B
As you can see, it's already sorted by numerical order, how do I sort it again, breaking the ties by using the alphabetical order of the second column, but... (2 Replies)
Discussion started by: Andrew9191
2 Replies
4. Shell Programming and Scripting
Hi to all.
I'm trying to sort this with the Unix command sort.
user1:12345678:3.5:2.5:8:1:2:3
user2:12345679:4.5:3.5:8:1:3:2
user3:12345687:5.5:2.5:6:1:3:2
user4:12345670:5.5:2.5:5:3:2:1
user5:12345671:2.5:5.5:7:2:3:1
I need to get this:
user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies
5. Shell Programming and Scripting
Hello,
okey so my script is using 4 variables that are either empty or numbers in the following format:
NUMBER_1 NUMBER_2 NUMBER_3 NUMBER_4
So they're basically separated by a space and I need to echo the lowest number, so far I've been doing it like this:
echo "2 3 1 3" | tr " "... (6 Replies)
Discussion started by: TehOne
6 Replies
6. Shell Programming and Scripting
I just upgraded my laptop from Fedora 13 to 14, and normally, before I do the upgrade (Fresh install) I run an rpm command to make a list of all the packages I have installed, but without the version and architectures specified, so that I can just feed that list to yum after the upgrade to... (2 Replies)
Discussion started by: Tsuroerusu
2 Replies
7. UNIX for Advanced & Expert Users
Hello all -
I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies
8. Shell Programming and Scripting
Hello Everybody :) !!!. i have question in mind, is it possible to sort a list of hexadecimal numbers using "sort" command? (9 Replies)
Discussion started by: Kesavan
9 Replies
9. Shell Programming and Scripting
Although i tried multiple option i couldn't find a way to get the rigt ouput.
Say i have the following data
cat file.txt
C request
C response
C request
C response
The output should look like (9 Replies)
Discussion started by: varu0612
9 Replies
10. Shell Programming and Scripting
I have files like this:
1
3
4
6
14
3
6
I want to extract the highest number. I have tried using
cat filename | sort
but then 9 would become higher than 14.
So how do I sort? (1 Reply)
Discussion started by: locoroco
1 Replies
LEARN ABOUT LINUX
dpkg::version
Dpkg::Version(3) libdpkg-perl Dpkg::Version(3)
NAME
Dpkg::Version - handling and comparing dpkg-style version numbers
DESCRIPTION
The Dpkg::Version module provides pure-Perl routines to compare dpkg-style version numbers (as used in Debian packages) and also an object
oriented interface overriding perl operators to do the right thing when you compare Dpkg::Version object between them.
OBJECT INTERFACE
my $v = Dpkg::Version->new($version, %opts)
Create a new Dpkg::Version object corresponding to the version indicated in the string (scalar) $version. By default it will accepts
any string and consider it as a valid version. If you pass the option "check => 1", it will return undef if the version is invalid (see
version_check for details).
You can always call $v->is_valid() later on to verify that the version is valid.
boolean evaluation
When the Dpkg::Version object is used in a boolean evaluation (for example in "if ($v)" or "$v || 'default'") it returns its string
representation if the version stored is valid ($v->is_valid()) and undef otherwise.
$v->is_valid()
Returns true if the version is valid, false otherwise.
$v->epoch(), $v->version(), $v->revision()
Returns the corresponding part of the full version string.
$v1 <=> $v2, $v1 < $v2, $v1 <= $v2, $v1 > $v2, $v1 >= $v2
Numerical comparison of various versions numbers. One of the two operands needs to be a Dpkg::Version, the other one can be anything
provided that its string representation is a version number.
"$v", $v->as_string()
Returns the string representation of the version number.
FUNCTIONS
All the functions are exported by default.
version_compare($a, $b)
Returns -1 if $a is earlier than $b, 0 if they are equal and 1 if $a is later than $b.
If $a or $b are not valid version numbers, it dies with an error.
version_compare_relation($a, $rel, $b)
Returns the result (0 or 1) of the given comparison operation. This function is implemented on top of version_compare().
Allowed values for $rel are the exported constants REL_GT, REL_GE, REL_EQ, REL_LE, REL_LT. Use version_normalize_relation() if you have
an input string containing the operator.
my $rel = version_normalize_relation($rel_string)
Returns the normalized constant of the relation $rel (a value among REL_GT, REL_GE, REL_EQ, REL_LE and REL_LT). Supported relations
names in input are: "gt", "ge", "eq", "le", "lt", ">>", ">=", "=", "<=", "<<". ">" and "<" are also supported but should not be used as
they are obsolete aliases of ">=" and "<=".
version_compare_string($a, $b)
String comparison function used for comparing non-numerical parts of version numbers. Returns -1 if $a is earlier than $b, 0 if they
are equal and 1 if $a is later than $b.
The "~" character always sort lower than anything else. Digits sort lower than non-digits. Among remaining characters alphabetic
characters (A-Za-z) sort lower than the other ones. Within each range, the ASCII decimal value of the character is used to sort between
characters.
version_compare_part($a, $b)
Compare two corresponding sub-parts of a version number (either upstream version or debian revision).
Each parameter is split by version_split_digits() and resulting items are compared together.in digits and non-digits items that are
compared together. As soon as a difference happens, it returns -1 if $a is earlier than $b, 0 if they are equal and 1 if $a is later
than $b.
my @items = version_split_digits($version)
Splits a string in items that are each entirely composed either of digits or of non-digits. For instance for "1.024~beta1+svn234" it
would return ("1", ".", "024", "~beta", "1", "+svn", "234").
my ($ok, $msg) = version_check($version)
my $ok = version_check($version)
Checks the validity of $version as a version number. Returns 1 in $ok if the version is valid, 0 otherwise. In the latter case, $msg
contains a description of the problem with the $version scalar.
AUTHOR
Don Armstrong <don@donarmstrong.com>, Colin Watson <cjwatson@debian.org> and Raphael Hertzog <hertzog@debian.org>, based on the
implementation in "dpkg/lib/vercmp.c" by Ian Jackson and others.
1.16.0.3 2012-04-17 Dpkg::Version(3)