使用命令提示符在文本文件中排序数字

时间:2015-08-31 06:46:27

标签: cmd

我想在一个文本文件中对数字进行排序并在同一个文本文件中写相同我使用下面给出的命令但是我无法获得输出可以有人请帮我一样

sort -n test.txt /o output.txt

由于输入文件指定了两次,因此会出现错误消息。

2 个答案:

答案 0 :(得分:0)

(很好运动:))

@echo off
setlocal enabledelayedexpansion

REM convert all numbers to the same lenght by adding leading spaces:
(for /f %%i in (input.txt) do (
    REM 12 leading spaces:
    set "Z=            %%i"
    REM take last 12 digits:
    echo !Z:~-12!
))>temp.txt 

REM sort it:'
sort temp.txt /O temp.txt

REM remove the spaces:
(for /f %%i in (temp.txt) do echo/%%i)>output.txt
del temp.txt

答案 1 :(得分:0)

确保 (很好的练习:)) !!!


使用命令提示符排序文本文件中的数字,但不使用sort 命令!**

@echo off && setlocal enabledelayedexpansion & cd /d "%~dp0" 

for /f delims^=:^ tokens^=^2 %%i in ('find /c /v ";" input.txt')do set _cnt=%%i
2>nul (cd.>"%tmp%\_file_.tmp" & cd.>"output.txt") & for /f %%i in (input.txt)do echo/,%%i,>>"%tmp%\_file_.tmp"
for /l %%L in (1 1 99999)do find ",%%L," "%tmp%\_file_.tmp" >nul && (echo/%%L>>"output.txt" & call set /a "_cnt-=1" && if !_cnt! == 0 goto :~0)

:~0  
timeout -1  & del "%tmp%\_file_.tmp" & type "output.txt"

:: to count lines / numbers that exist in the file, too, to be used as a run delimiter in for /l ::
for /f delims^=:^ tokens^=^2 %%i in ('find /c /v ";" input.txt')do set _cnt=%%i

:: create the 2 empty files and if they exist, delete the contents ::
2>nul (cd.>"%tmp%\_file_.tmp" & cd.>"output.txt")

:: create unique string for each file number/line "%tmp%\_file_.tmp" ::
for /f %%i in (input.txt)do echo/,%%i,>>"%tmp%\_file_.tmp"

rem :: performs a 1 in 1 to 99999 numeric loop and also finds ",number," in the file "%tmp%\_file_.tmp" ::
rem :: remove ",," (commas) from string, save (if any), only the variable number %%L in "output.txt"
for /l %%L in (1 1 99999)do find ",%%L," "%tmp%\_file_.tmp" >nul && (echo/%%L>>"output.txt" & call set /a "_cnt-=1" && if !_cnt! == 0 goto :~0)

:: for each occurrence, it will be subtracting 1 from the _cnt counter, so when it reaches zero it stops listing/fetching ::
echo/%%L>>"output.txt" & call set /a "_cnt-=1" && if !_cnt! == 0 goto :~0)
相关问题