批处理命令查找字符串集中的最大数字?

时间:2017-01-20 14:18:15

标签: batch-file cmd highest

例如:

file.txt包含:

4.3 - random1
5.6 - random2
2.2 - random3
3 - random4
1.8 - random5

我需要一个只输出最高数字的命令,而不是前面的文本。

IE中。

Output = 5.6

3 个答案:

答案 0 :(得分:1)

sort将按正确顺序排序(注意:这是排序字符串,而不是数字,但对于您的示例可以正常工作。请注意,字符串比较56.3是“大于“15”。

放置一个for来处理输出(标准标记为1,空格是标准分隔符,因此for /f只获取第一个元素 - 您想要的数字)

for /f %%a in ('sort t.txt') do set high=%%a
echo %high%

编辑也可以处理高于10的数字。注意:没有涉及数学 - 它只是巧妙的字符串操作。

@echo off
setlocal enabledelayedexpansion

(
  for /f "tokens=1,2 delims=. " %%a in (t.txt) do (
    set a=0000%%a
    if "%%b"=="-" (echo !a:~-4!) else (echo !a:~-4!.%%b)
  )
)>temp.txt
type temp.txt
pause
for /f "tokens=1,2 delims=0" %%a in ('sort temp.txt') do set high=%%a

echo %high%

答案 1 :(得分:0)

您可以尝试SORTN.bat

以下是它的代码。

@ECHO OFF
if "%~1"=="/?" (
    echo.Sorts text by handling first number in line as number not text
    echo.
    echo.%~n0 [n]
    echo.
    echo.  n     Specifies the character number, n, to
    echo.        begin each comparison.  3 indicates that
    echo.        each comparison should begin at the 3rd
    echo.        character in each line.  Lines with fewer
    echo.        than n characters collate before other lines.
    echo.        By default comparisons start at the first
    echo.        character in each line.
    echo.
    echo.Description:
    echo.        'abc10def3' is bigger than 'abc9def4' because
    echo.        first number in first string is 10
    echo.        first number in second string is 9
    echo.        whereas normal text compare returns 
    echo.        'abc10def3' smaller than 'abc9def4'
    echo.
    echo.Example:
    echo.        To sort a directory pipe the output of the dir
    echo.        command into %~n0 like this:
    echo.           dir /b^|%~n0
    echo.
    echo.Source: http://www.dostips.com
    goto:EOF
)
if "%~1" NEQ "~" (
    for /f "tokens=1,* delims=," %%a in ('"%~f0 ~ %*|sort"') do echo.%%b
    goto:EOF
)
SETLOCAL ENABLEDELAYEDEXPANSION
set /a n=%~2+0
for /f "tokens=1,* delims=]" %%A in ('"find /n /v """') do (
    set f=,%%B
    (
        set f0=!f:~0,%n%!
        set f0=!f0:~1!
        rem call call set f=,%%%%f:*%%f0%%=%%%%    
        set f=,!f:~%n%!
    )
    for /f "delims=1234567890" %%b in ("!f!") do (
        set f1=%%b
        set f1=!f1:~1!
        call set f=0%%f:*%%b=%%
    )
    for /f "delims=abcdefghijklmnopqrstuwwxyzABCDEFGHIJKLMNOPQRSTUWWXYZ~`@#$*_-+=:;',.?/\ " %%b in ("!f!") do (
        set f2=00000000000000000000%%b
        set f2=!f2:~-20!
        call set f=%%f:*%%b=%%
    )
    echo.!f1!!f2!!f!,%%B
    rem echo.-!f0!*!f1!*!f2!*!f!*%%a>&2
)

我尝试使用此输入作为示例

4.3 - random1
11.3 - random6
5.6 - random2
2.2 - random3
100.1 - random8
3 - random4
1.8 - random5
11.12 - random7
11.11 - random7

这就是我运行它的方式,但是您应该能够使用FOR /F命令捕获输出,就像Stephan在回答中给出的那样。

type sortme.txt |sortn.bat

输出

1.8 - random5
2.2 - random3
3 - random4
4.3 - random1
5.6 - random2
11.11 - random7
11.12 - random7
11.3 - random6
100.1 - random8

答案 2 :(得分:0)

以下脚本最多对小数分隔符x[n][n]之前和之后的八位数进行正确排序:

.