China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 21:21
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Resolved] Can sort be used to sort file values by size View 2,078 Replies 6
Original Poster Posted 2007-05-17 20:12 ·  中国 福建 福州 连江县 联通
高级用户
★★
Credits 581
Posts 277
Joined 2006-12-23 05:10
19-year member
UID 74328
Gender Male
Status Offline
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 ]
Floor 2 Posted 2007-05-17 20:48 ·  中国 福建 福州 连江县 联通
高级用户
★★
Credits 581
Posts 277
Joined 2006-12-23 05:10
19-year member
UID 74328
Gender Male
Status Offline
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 ]
Floor 3 Posted 2007-05-22 21:02 ·  中国 青海 西宁 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
I'm also thinking about this question, and there's no answer either
Floor 4 Posted 2007-05-23 00:59 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Search more, I remember there was a similar discussion before.
Floor 5 Posted 2007-05-23 01:19 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Try the following code:

::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)

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

25
1
9
385
87
195876

The test result under CMD is as follows:

F:\>paixu.bat test.txt
1
9
25
87
385
195876


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.....
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
plp626 +8 2008-03-22 05:51
Floor 6 Posted 2008-05-03 11:13 ·  中国 北京 海淀区 联通
初级用户
Credits 28
Posts 15
Joined 2008-04-28 09:18
18-year member
UID 117150
Gender Male
Status Offline
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`???????
Floor 7 Posted 2008-12-27 13:51 ·  中国 江苏 南京 电信
初级用户
★★
Credits 62
Posts 62
Joined 2007-07-14 14:39
19-year member
UID 93962
Gender Male
Status Offline
Hehe, how to input this sorting result into a new text file? Please teach me hehe
Forum Jump: