Unix Version Numbers

I have a list of version numbers, say, for example, that they are in the version.txt file

1.2.100.4 1.2.3.4 10.1.2.3 9.1.2.3 

I want to sort them so that they are sorted by version. i.e:

 1.2.3.4 1.2.100.4 9.1.2.3 10.1.2.3 

I tried using various sorting commands using the "k" options, but I really don't understand them well enough to disable it. Any help would be greatly appreciated.

+42
sorting unix
Dec 20 '10 at 19:39
source share
7 answers

The -V option is most enjoyable, but I wanted to stay away from installing new / different software, since my sorting did not have this option.

This is the team that worked for me at the end:

 sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n test.txt 

From the comments:

  • To reorder: sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr
  • To skip the v : sort -t. -k 1.2,1n -k 2,2n -k 3,3n -k 4,4n prefix sort -t. -k 1.2,1n -k 2,2n -k 3,3n -k 4,4n sort -t. -k 1.2,1n -k 2,2n -k 3,3n -k 4,4n
+85
Dec 21 2018-10-12T00:
source share
 sort -V versions.txt 

From man sort :

-V , --version-sort
natural look (version) in the text

See also About sorting versions .

+51
Dec 20 '10 at 19:45
source share

BSD does not provide -V by default, so Ben's solution is as close as possible. For your convenience, I post here our version, which is able to sort files of the type <label>-<version>.<ext> :

 % ls bla-*.ime | sed -Ee 's/^(.*-)([0-9.]+)(\.ime)$/\2.-1 \1\2\3/' | sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 | cut -d\ -f2- bla-1.ime bla-1.0.ime bla-1.0.0.ime bla-1.1.ime bla-1.1.29.ime bla-1.2.3.ime bla-1.2.29.ime bla-1.2.30.ime bla-1.3.ime bla-1.3.0.ime bla-1.3.1.ime bla-1.3.10.ime bla-1.3.20.ime bla-1.7.ime bla-1.11.29.ime bla-2.3.2.ime bla-11.2.2.ime 

Brief explanation:

  • List the files you want to sort with ls .
  • Find the version number and line prefix.
  • In this case, add -1 to the end to first make the sorting of the shorter version (up to .0 even). You can change -1 to 0 if you think 1.3 equivalent to 1.3.0 .
  • Sort the strings using the proposed Ben solution by version number.
  • Disable version prefix from string.

The list now contains a list of file names sorted by list. Any additional sorting in the label part remains as an exercise for the reader.

+3
Jun 02 '14 at 11:28
source share

In Perl:

 sub compare_version_numbers { my ($l,$r) = @_; my @lx = split("\\.",$l); my @rx = split("\\.",$r); my $minlen = (@lx < @rx) ? @lx : @rx; for (my $i=0; $i < $minlen; $i++) { # make numeric by multiplying with 1 my $l_number = ($lx[$i] * 1); my $r_number = ($rx[$i] * 1); # compare with spaceship operator my $l_vs_r = ($l_number <=> $r_number); # return if decision is clear! if ($l_vs_r != 0) { return $l_vs_r } # otherwise, next part in array of version numbers } # if we are here, we could not decide - shortest entry wins! return @lx <=> @rx } 
0
Feb 09 '14 at 16:56
source share

This command:

 echo "1.2.100.4,1.2.3.4,10.1.2.3,9.1.2.3" | tr ',' '\n' | sort -V 

Gives output:

 1.2.3.4 1.2.100.4 9.1.2.3 10.1.2.3 
0
May 29 '15 at
source share
 sort -n <versions.txt 

-3
Dec 20 '10 at 19:42
source share
 echo "1.2.100.4,1.2.3.4,10.1.2.3,9.1.2.3" | tr ',' '\n' | sort -k1,1n 

Output:

1.2.100.4
1.2.3.4
9.1.2.3
10.1.2.3

You should be able to figure out the rest. Good luck.

-3
Feb 13 '13 at 4:08
source share



All Articles