简体   繁体   English

Unix 版本号排序

[英]Unix sort of version numbers

I have a list of version numbers, let's say for instance that they are in a file versions.txt我有一个版本号列表,例如,它们在文件 versions.txt 中

1.2.100.4
1.2.3.4
10.1.2.3
9.1.2.3

I wish to sort them so that they are sorted by version.我希望对它们进行排序,以便它们按版本排序。 ie: IE:

1.2.3.4
1.2.100.4
9.1.2.3    
10.1.2.3

I have tried using various sort commands using the "k" parameters, but do not really understand it well enough to pull it off.我已经尝试使用各种使用“k”参数的排序命令,但并没有真正理解到足以实现它。 Any help would be appreciated.任何帮助,将不胜感激。

The -V option is the nicest, but I wanted to stay away from installing new/other software since my sort didn't have that option. -V选项是最好的,但我不想安装新的/其他软件,因为我的类型没有该选项。

This is the command that worked for me in the end:这是最终对我有用的命令:

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

From comments:来自评论:

  • To reverse the order: 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 sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr
  • To skip the v prefix: sort -t. -k 1.2,1n -k 2,2n -k 3,3n -k 4,4n要跳过v前缀: 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
sort -V versions.txt

From man sort :man sort

-V , --version-sort -V , --version-sort
natural sort of (version) numbers within text文本中自然排序的(版本)数字

See also Details about version sort .另请参阅有关版本排序的详细信息

BSD does not provide -V by default, so Ben's solution is as close as it gets. BSD 默认不提供-V ,所以 Ben 的解决方案已经很接近了。 For your convenience I post here our version that is able to sort files like <label>-<version>.<ext> :为了您的方便,我在这里发布了我们的版本,它能够像<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

Short explanation:简短说明:

  • List the files that you want to sort with ls .列出要使用ls排序的文件。
  • Find the version number and prefix the line with that.找到版本号并在行前加上它。
  • While doing that add -1 to the end to make shorter version number sort first (before .0 even).在执行此操作时,将-1添加到末尾以使较短的版本号首先排序(甚至在.0之前)。 You could change -1 to 0 if you consider 1.3 to be equivalent to 1.3.0 .如果您认为1.3等同于1.3.0 ,则可以将-1更改为0
  • Sort the lines using Ben's suggested solution on the version number.使用 Ben 建议的版本号解决方案对行进行排序。
  • Chop off the version prefix from the line.从行中删除版本前缀。

The list now contains a version sorted list of applicable file names.该列表现在包含适用文件名的版本排序列表。 Any additional sorting on the label part is left as an exercise to the reader. label部分的任何额外排序留给读者作为练习。

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

In Perl:在 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
}
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.100.4
1.2.3.4 1.2.3.4
9.1.2.3 9.1.2.3
10.1.2.3 10.1.2.3

You should be able to figure out the rest.您应该能够弄清楚其余部分。 Good luck祝你好运

sort -n <versions.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM