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-06-27 09:28
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » What is the difference in usage between tokens=* and delims= in the for command? View 2,116 Replies 14
Original Poster Posted 2007-02-09 09:19 ·  中国 江苏 连云港 联通
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
Using for to analyze the content of a certain text. In case the lines separated by spaces in the text are broken and the strings after the spaces are cut off, I see some people use "tokens=*" and some use "delims=", and some people use both. What are the differences in these two usages? I personally understand that the effects should be the same. "tokens=*" means taking all fields, "delims=" means not specifying any delimiter. If there are spaces in the first line of the text to break it, using one of them can make the analyzed string not break, right? Is it that you can use either one according to personal preference, or are there some tips in the usage of these two? I hope the friends who know will tell me, thank you for replying in your busy schedule!

[ Last edited by oilio on 2007-2-8 at 08:51 PM ]
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 2 Posted 2007-02-09 21:39 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Just try it yourself.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 3 Posted 2007-03-20 23:41 ·  中国 上海 电信
初级用户
Credits 21
Posts 9
Joined 2005-08-29 11:21
20-year member
UID 42047
Status Offline
delims refers to the delimiter, and tokens refers to the selected string fragments after splitting.

Take the following code as an example:
FOR /F "eol=; tokens=2,3* delims=," %i in ("a,bc,def,ghij,jklmn") do @echo %i %j %k
Here, Delims is ",", so "a,bc,def,ghij,klmn") is split into 5 segments: a bc def ghij klmn.
tokens=2,3* means to take the 2nd fragment, the 3rd fragment, and all fragments after the 3rd fragment. That is:
%i=bc
%j=def
%k=ghij,klmn (here "*" means that from the 4th fragment onwards, no more splitting is done, all are assigned to %k)
So if you use "tokens=*" in your code, it means no splitting is performed, and the current line is directly assigned to the loop variable.

[ Last edited by lelige on 2007-3-20 at 11:42 PM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
dosjj +2 2008-11-04 09:25
dosz +1 2009-03-07 09:34
Floor 4 Posted 2007-03-21 00:05 ·  中国 广东 广州 黄埔区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Floor 5 Posted 2007-03-21 01:25 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
It can simply give you an example. For example, there is a text a.txt with the following content:
hello world
thank you very
you are very good

These three lines of content
for /f "tokens=*" %a in (a.txt) do @echo %a
hello world
thank you very
you are very good

It can indirectly delete the spaces in front of each line

for /f "delims=" %a in (a.txt) do @echo %a

hello world
thank you very
you are very good

Still the original text, heh heh. You can explore the specific parameters and applications inside by yourself
Recent Ratings for This Post ( 4 in total) Click for details
RaterScoreTime
oilio +3 2007-03-21 02:15
logictianjin +2 2007-04-11 03:43
Climbing +10 2008-03-23 00:07
dosjj +2 2008-11-04 09:30
我今后在论坛的目标就是做个超级坏人!!!
Floor 6 Posted 2007-03-21 02:16 ·  中国 江苏 连云港 电信
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
Thank you, brother upstairs. I seem to have grasped a little bit of the essence.
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 7 Posted 2007-10-12 23:10 ·  中国 浙江 温州 电信
初级用户
★★
Credits 158
Posts 89
Joined 2007-04-25 08:06
19-year member
UID 86402
Gender Male
Status Offline
First, top it, and learn slowly
Floor 8 Posted 2007-10-13 10:39 ·  中国 江西 南昌 电信
中级用户
★★
Credits 245
Posts 123
Joined 2007-06-08 09:04
19-year member
UID 90636
Gender Female
Status Offline
Learning
Floor 9 Posted 2008-03-03 23:51 ·  中国 重庆 电信
初级用户
Credits 34
Posts 14
Joined 2007-12-13 18:21
18-year member
UID 105563
Gender Male
Status Offline
Floor 10 Posted 2008-03-22 17:42 ·  中国 广东 广州 电信
中级用户
★★
Credits 233
Posts 117
Joined 2007-11-28 02:38
18-year member
UID 104005
Gender Male
Status Offline
After seeing the 3rd floor, I gradually understood, thank you
Floor 11 Posted 2008-03-28 15:50 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
It is suggested to ban the ID of the person upstairs and delete all their posts.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 12 Posted 2009-03-07 10:44 ·  中国 上海 浦东新区 电信
中级用户
★★
一看二跟三动手
Credits 396
Posts 188
Joined 2005-10-23 13:21
20-year member
UID 43919
From 上海
Status Offline
Originally posted by Climbing at 2007-2-9 21:39:
Just try it yourself.


Helping others improve themselves is beneficial to both others and oneself
@echo off
title Usage of "tokens=2 and delims=," in the for command
echo.
echo Before the bed the bright moon shines, I think frost on the ground it is. Raising my head, I watch the bright moon, lowering my head, I think of home.
echo.
echo If I want to display all four lines of the poem on the screen using for without any parameters, it can be achieved:
echo for /f %%i in (JingYeSi.txt) do echo %%i
ECHO 1-1 Before the bed the bright moon shines, 1-2 I think frost on the ground it is, 1-3 Raising my head, I watch the bright moon, 1-4 Lowering my head, I think of home.> JingYeSi.txt
ECHO 2-1 Before the bed the bright moon shines, 2-2 I think frost on the ground it is, 2-3 Raising my head, I watch the bright moon, 2-4 Lowering my head, I think of home.>>JingYeSi.txt
ECHO 3-1 Before the bed the bright moon shines, 3-2 I think frost on the ground it is, 3-3 Raising my head, I watch the bright moon, 3-4 Lowering my head, I think of home.>>JingYeSi.txt
for /f %%i in (JingYeSi.txt) do echo %%i
pause>nul
echo.
ECHO If I only want the sentence "Before the bed the bright moon shines", I need the help of delims:
ECHO for /f "delims=," %%i in (JingYeSi.txt) do echo %%i
for /f "delims=," %%i in (JingYeSi.txt) do echo %%i
ECHO delims= followed by a comma means using the comma in the original text as the delimiter (the method of division) to divide the ancient poem into four sections. By default, delims only displays the first section before the delimiter, and the delimiter is usually ignored.
pause>nul
echo.
ECHO To display the verses of other sections, the column extraction command "tokens" is used.
ECHO for /f "tokens=2 delims=," %%i in (JingYeSi.txt) do echo %%i
for /f "tokens=2 delims=," %%i in (JingYeSi.txt) do echo %%i
ECHO tokens=2 means the second character set of the object (here, each line)
pause>nul


[ Last edited by dosz on 2009-3-7 at 10:51 ]
Attachments
for命令中 tokens=2 和 delims=, 的用法.PNG
*/. . . * .
.\* . * dosz
*/ . ./\~~~~~~~~~~~~'\. ^|◆
\* ,/,..,\,...........,\.◆
^|^| ..▎# ▎田 田 ▎ ^| ▎◆
^|^| ^&^&▎ ▎ ▎'^|'▎ o
^|^| ##■■■■■■■■■■〓
Floor 13 Posted 2009-03-07 11:00 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
tokens= When the item is empty, only take the first item,
delims= When the item is empty (default), use spaces and [Tab] as delimiters,
精简
=> 个人网志
Floor 14 Posted 2009-04-17 23:57 ·  中国 上海 中移铁通
新手上路
Credits 2
Posts 2
Joined 2009-04-13 04:29
17-year member
UID 142809
Gender Male
Status Offline
Very good, very powerful, I've learned
Floor 15 Posted 2009-04-24 10:57 ·  中国 上海 浦东新区 电信
新手上路
Credits 3
Posts 3
Joined 2009-04-21 09:28
17-year member
UID 143439
Gender Male
Status Offline
I've always been unfamiliar with for, but now I'm a bit closer to it!
THANK ALL!
Forum Jump: