中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-15 12:31
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » [Resolved] Can sort be used to sort file values by size
Printable Version  2,233 / 6
Floor1 suntb Posted 2007-05-17 20:12
高级用户 Posts 277 Credits 581
RT

Suppose the file test.txt contains purely numerical data, with the following numbers:

25

1

9

385

87

195876

Using the command sort test.txt only gives the following result:

1

195876

25

385

87

9

That is, when sort is used for sorting, it first sorts by the first character. Then if we want to sort according to the usual numerical size, how should we handle it? The expected result should be as follows:

1

9

25

87

385

195876


[ Last edited by suntb on 2007-5-18 at 10:05 AM ]
Floor2 suntb Posted 2007-05-17 20:48
高级用户 Posts 277 Credits 581
No one person to help, check the posts in the forum, it seems that sort cannot be realized

[ Last edited by suntb on 2007-5-18 at 11:11 AM ]
Floor3 vkill Posted 2007-05-22 21:02
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
I'm also thinking about this question, and there's no answer either
Floor4 lxmxn Posted 2007-05-23 00:59
版主 Posts 4,938 Credits 11,386
Search more, I remember there was a similar discussion before.
Floor5 youxi01 Posted 2007-05-23 01:19
高级用户 Posts 247 Credits 846 From 湖南==》广东
Try the following code:

Usage format:
Example: There are numbers in a file named "test.txt" as follows:

The test result under CMD is as follows:


But the depressing thing is that when I changed the P processing file name to sort.bat during the test, then the problem came, which made me restart twice, and then I finally understood.....
Floor6 MLQX Posted 2008-05-03 11:13
初级用户 Posts 15 Credits 28
In the code, the reason for adding the "OK" letters is related to the logic of the `for /f` loop processing. When the code is structured like `"%~0" %1 OK`, it provides additional input content to the script when it is recursively called. The `sort` command then sorts the output, and the `tokens=2 delims=#` is used to extract the second part of the content separated by the `#` delimiter. So the "OK" is just part of the input passed to the script during this recursive and processing flow.

The translated text remains the same as the original code content since it's already in a form that can be presented directly after translation processing:

::code by youxi01@cn-dos.net
::Usage format: In the command line, enter:
::
@echo off
setlocal enabledelayedexpansion

if "%2"=="" (
for /f "tokens=2 delims=#" %%i in ('"%~0" %1 OK^|sort') do echo %%i
) else (
for /f %%i in (%1) do (
call :CheckNum %%i Res
echo !Res!#%%i)
goto :eof)

pause>nul

:CheckNum num Res
set/a Res=0
for /l %%i in (0 1 1000) do (
set "var=%~1"
set tmp=!var:~%%i,1!
if "!tmp!"=="" goto :eof
set/a Res+=1


Why is the "OK" letter added in `for /f "tokens=2 delims=#" %%i in ('"%~0" %1 OK^|sort') do echo %%i`???????
Floor7 pkto Posted 2008-12-27 13:51
初级用户 Posts 62 Credits 62
Hehe, how to input this sorting result into a new text file? Please teach me hehe
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023