如何含有格式major.minor.build.revision数值版本号的文本文件的行进行排序? [英] How to sort lines of a text file containing version numbers in format major.minor.build.revision numerical?

查看:1573
本文介绍了如何含有格式major.minor.build.revision数值版本号的文本文件的行进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的价值观的TXT文件:

I have a txt file with values like this:

3.6.4.2
3.6.5.1
3.6.5.10
3.6.5.11
3.6.5.12
3.6.5.13
3.6.5.2
3.6.7.1
3.6.7.10
3.6.7.11
3.6.7.2
3.6.7.3

我需要写一个批处理脚本,并返回一个排序的输出。问题是与最后一列,数字0.10和0.11 0.3至打完应该去。我需要最新版是在底部,而在这种情况下,是3.6.7.11

I need to write a batch script and return a sorted output. The problem is with last column, numbers .10 and .11 should go after .3 and so. I need the "latest version" to be on the bottom, which in this case is 3.6.7.11

在Linux的我用之类的-t,-k1n,1 -k2n,2 -k3n,3 -k4n,4,但我不能让它与批处理脚本工作。

In Linux I used "sort -t"." -k1n,1 -k2n,2 -k3n,3 -k4n,4" but I can't get it working with batch script.

另外,我不允许使​​用Cygwin或PowerShell的一些原因。

Also I am not allowed to use Cygwin or PowerShell for some reasons.

在我的批处理code,我至今想只有这样各种版本,但没有什么工作对我来说:

In my batch code I am so far trying only various versions of this but nothing is working for me:

sort /+n versions.txt

在这个问题上使用的输出很简单

The output used in this question is simply

sort versions.txt

它看起来像命令排序正确地做它,直到我不能用2位数做的。

It looks like that command sort is doing it correctly until I don't have 2 digits number used.

推荐答案

这是在批处理文件中的一个常见问题。所有排序方法使用的的比较,其中10到来之前2,因此有必要在不到10下批处理文件中的编号,以插入左零做到这一点,但不是生成具有固定号码一个新的文件,它使用它们创建的阵列的将被自动排序。在此之后,该数组元素显示在其自然(排序)顺序

This is a common problem in Batch files. All sorting methods use a string comparison, where "10" comes before "2", so it is necessary to insert left zeros in the numbers less than 10. The Batch file below do that, but instead of generate a new file with the fixed numbers, it uses they to create an array that will be automatically sorted. After that, the array elements are shown in its natural (sorted) order.

修改:我修改了code,才能在四个部分来管理两个数字编号

EDIT: I modified the code in order to manage two digits numbers in the four parts.

@echo off
setlocal EnableDelayedExpansion

for /F "tokens=1-4 delims=." %%a in (input.txt) do (
    rem Patch the four numbers as a two digits ones
    set /A "a=100+%%a, b=100+%%b, c=100+%%c, d=100+%%d"
    rem Store line in the proper array element
    set "line[!a:~1!!b:~1!!c:~1!!d:~1!]=%%a.%%b.%%c.%%d"
)

rem Show array elements
for /F "tokens=2 delims==" %%a in ('set line[') do echo %%a

输出:

3.6.4.2
3.6.5.1
3.6.5.2
3.6.5.10
3.6.5.11
3.6.5.12
3.6.5.13
3.6.7.1
3.6.7.2
3.6.7.3
3.6.7.10
3.6.7.11

这篇关于如何含有格式major.minor.build.revision数值版本号的文本文件的行进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆