How to sort lines of a text file containing version numbers in format major.minor.build.revision numerical?

后端 未结 5 732
忘掉有多难
忘掉有多难 2020-12-04 04:06

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
5条回答
  •  渐次进展
    2020-12-04 04:25

    Here is my solution working with 2 temporary files which works also if one of the other 3 version numbers becomes ever greater than 9.

    @echo off
    setlocal EnableExtensions EnableDelayedExpansion
    
    set "VersionsFile=versions.txt"
    
    rem Delete all temporary files perhaps existing from a previous
    rem run if user of batch file has broken last batch processing.
    
    if exist "%TEMP%\%~n0_?.tmp" del "%TEMP%\%~n0_?.tmp"
    
    rem Insert a leading 0 before each number in version string if the
    rem number is smaller than 10. And insert additionally a period at
    rem start of each line. The new lines are written to a temporary file.
    
    for /F "useback tokens=1-4 delims=." %%A in ("%VersionsFile%") do (
        if %%A LSS 10 ( set "Line=.0%%A." ) else ( set "Line=.%%A." )
        if %%B LSS 10 ( set "Line=!Line!0%%B." ) else ( set "Line=!Line!%%B." )
        if %%C LSS 10 ( set "Line=!Line!0%%C." ) else ( set "Line=!Line!%%C." )
        if %%D LSS 10 ( set "Line=!Line!0%%D" ) else ( set "Line=!Line!%%D" )
        echo !Line!>>"%TEMP%\%~n0_1.tmp"
    )
    
    rem Sort this temporary file with output written to one more temporary file.
    rem The output could be also printed to __stdout__ and directly processed.
    
    %SystemRoot%\System32\sort.exe "%TEMP%\%~n0_1.tmp" /O "%TEMP%\%~n0_2.tmp"
    
    rem Delete the versions file before creating new with sorted lines.
    
    del "%VersionsFile%"
    
    rem Read sorted lines, remove all leading zeros after a period and also
    rem the period inserted at start of each line making it easier to remove
    rem all leading zeros. The lines are written back to the versions file.
    
    for /F "useback delims=" %%L in ("%TEMP%\%~n0_2.tmp") do (
        set "Line=%%L"
        set "Line=!Line:.0=.!"
        set "Line=!Line:~1!"
        echo !Line!>>"%VersionsFile%"
    )
    
    rem Finally delete the two temporary files used by this batch file.
    
    del "%TEMP%\%~n0_?.tmp" >nul
    
    endlocal
    

    The first temporary file with unsorted lines contains for input example:

    .03.06.04.02
    .03.06.05.01
    .03.06.05.10
    .03.06.05.11
    .03.06.05.12
    .03.06.05.13
    .03.06.05.02
    .03.06.07.01
    .03.06.07.10
    .03.06.07.11
    .03.06.07.02
    .03.06.07.03
    

    The second temporary file with the sorted lines contains for input example:

    .03.06.04.02
    .03.06.05.01
    .03.06.05.02
    .03.06.05.10
    .03.06.05.11
    .03.06.05.12
    .03.06.05.13
    .03.06.07.01
    .03.06.07.02
    .03.06.07.03
    .03.06.07.10
    .03.06.07.11
    

    For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

    • call /? ... explains %~n0 (name of batch file without path and file extension)
    • del /?
    • echo /?
    • endlocal /?
    • for /?
    • if /?
    • rem /?
    • set /?
    • setlocal /?
    • sort /?

提交回复
热议问题