|
lgaint
初级用户
 
积分 22
发帖 7
注册 2006-10-25
状态 离线
|
『楼 主』:
For 命令中tokens参数的使用求教?
使用 LLM 解释/回答一下
因为要批量建文件夹,学习了一下FOR的使用,但任务完成了,FOR的学习却没法完成,被TOKEN绊了个跟头~~~
在命令行中打for /?出来的解释看不在懂,请问大家一下:
eol=c - 指一个行注释字符的结尾(就一个) ________这个俺懂,指明什么是注释行,忽略过
skip=n - 指在文件开始时忽略的行数。 _________这个也明白
delims=xxx - 指分隔符集。这个替换了空格和跳格键的默认分隔符集。___________这个似乎明白,在试验中,发现似乎是指从指定的字符处开始干活
tokens=x,y,m-n - 指每行的哪一个符号被传递到每个迭代的 for 本身。这会导致额外变量名称的格式为一个范围。通过 nth 符号指定 m符号。如果字符串中的最后一个字符星号,那么额外的变量将在最后一个符号解析之后,分配并接受行的保留文本。 _____________这个就完全糊涂了,越试越糊涂,一团浆糊.
tokens=x,y,m-n
tokens的使用参数,我不明白x,y,m-n都是什么作用,哪位达人给解释一下?
如果您有时间,请向下接着看,如果没时间,就此指点两句也行,谢谢了先.
这个是我用来做试验的文本文件t1.txt的内容
为a工了挽住那转逝的感触,
我a不得不提起了笔,
写a下这禁忌之爱的点点滴滴。
或a许阴暗,或许残忍,或许另类,
但a谁也不能否认这其间人物最真的善良与无私。
愿a大家陪着他们走到那注定的终点。
以下是试验过程
E:\>for /f "eol=为 tokens=2 delims=a " %1 in (t1.txt) do @echo %1
不得不提起了笔,
下这禁忌之爱的点点滴滴。
许阴暗,或许残忍,或许另类,
谁也不能否认这其间人物最真的善良与无私。
大家陪着他们走到那注定的终点。
E:\>for /f "eol=为 tokens=1 delims=a " %1 in (t1.txt) do @echo %1
我
写
或
但
愿
E:\>for /f "eol=为 tokens=3 delims=a " %1 in (t1.txt) do @echo %1
E:\>
Because I need to create folders in batches, I learned about the use of FOR, but the task is done, but the study of FOR can't be completed, and I was tripped up by TOKEN~~~
When I type for /? in the command line, the explanation is not understandable. Please ask everyone:
eol=c - refers to the end of a line comment character (just one) ________I understand this, it indicates what is a comment line and is ignored
skip=n - refers to the number of lines to ignore at the beginning of the file. ________This is also clear
delims=xxx - refers to the delimiter set. This replaces the default delimiter set of spaces and tabs. ________This seems to be clear. In the experiment, it seems to refer to starting to work from the specified character
tokens=x,y,m-n - refers to which symbol of each line is passed to the for itself in each iteration. This leads to the format of additional variable names as a range. Specify the m symbol through the nth symbol. If the asterisk is the last character in the string, then the additional variable will be assigned and accept the reserved text of the line after the last symbol is parsed. ________This is completely confused. The more I test, the more confused I am, a complete mess.
tokens=x,y,m-n
I don't understand the functions of x, y, m-n in the usage parameters of tokens. Which expert can explain it?
If you have time, please continue to read below. If you don't have time, just give two pointers. Thank you first.
This is the content of the text file t1.txt that I used for the experiment
In order to hold back the fleeting feelings,
I have to pick up the pen,
Write down the bits and pieces of this forbidden love.
Maybe dark, maybe cruel, maybe alternative,
But no one can deny the truest kindness and selflessness of the characters in it.
May everyone accompany them to the destined end.
The following is the test process
E:\>for /f "eol=为 tokens=2 delims=a " %1 in (t1.txt) do @echo %1
Have to pick up the pen,
Write down the bits and pieces of this forbidden love.
Maybe dark, maybe cruel, maybe alternative,
No one can deny the truest kindness and selflessness of the characters in it.
Everyone accompanies them to the destined end.
E:\>for /f "eol=为 tokens=1 delims=a " %1 in (t1.txt) do @echo %1
I
Write
Maybe
But
May
E:\>for /f "eol=为 tokens=3 delims=a " %1 in (t1.txt) do @echo %1
E:\>
|
|
2006-10-25 04:16 |
|
|
trickster
初级用户
 
积分 63
发帖 33
注册 2006-12-4
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
tokens=x,y,m-n - 指每行的哪一个符号被传递到每个迭代的 for 本身。这会导致额外变量名称的分配。m-n格式为一个范围。通过 nth 符号指定 mth。如果符号字符串中的最后一个字符星号,那么额外的变量将在最后一个符号解析之后分配并接受行的保留文本。
当tokens后指定了多个符号时,后面的变量也会进行扩充,比如下例:
for /f "tokens=1-4 delims=- " %a in ('date /t') do echo %a:%b:%c:%d
运行后我们得到"2006:12:29:星期五".其中,这个 for 程序体的语句引用 %a 来取得第一个符号(2006),引用 %b 来取得第二个符号(12),引用 %c来取得第三个符号(29),引用%d来取得第四个符号(星期五), 因为你用"tokens"指定了"date /t"命令输出的结果中的第一到第四个符号,所以这时引出了%b;%c;%d,当然后面的变量也不是随便给出的,它必须跟第一次给出的变量名是相邻的,但后面(do后面执行的命令)他们之间的顺序可以颠倒,只是会影响到最终的输出结果,如果在for /f中没有指定"tokens",默认 tokens=1
tokens=x,y,m-n - refers to which symbol in each line is passed to the for itself in each iteration. This leads to the assignment of additional variable names. The m-n format is a range. The m-th symbol is specified by the nth. If the last character in the symbol string is an asterisk, then the additional variable will be assigned and accept the remaining text of the line after the last symbol is parsed.
When multiple symbols are specified after tokens, the subsequent variables are also expanded, for example in the following example:
for /f "tokens=1-4 delims=- " %a in ('date /t') do echo %a:%b:%c:%d
After running, we get "2006:12:29:Friday". In this for program body statement, %a is used to get the first symbol (2006), %b is used to get the second symbol (12), %c is used to get the third symbol (29), and %d is used to get the fourth symbol (Friday). Because you specified "tokens" to get the first to fourth symbols in the output of the "date /t" command, then %b; %c; %d are introduced. Of course, the subsequent variables are not given randomly; they must be adjacent to the first variable name given. But the order between them in the command executed after do can be reversed, which will only affect the final output result. If "tokens" is not specified in for /f, the default is tokens=1
|
|
2007-1-1 05:08 |
|
|
trickster
初级用户
 
积分 63
发帖 33
注册 2006-12-4
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
这个是我用来做试验的文本文件t1.txt的内容
Quote:
为a工了挽住那转逝的感触,
我a不得不提起了笔,
写a下这禁忌之爱的点点滴滴。
或a许阴暗,或许残忍,或许另类,
但a谁也不能否认这其间人物最真的善良与无私。
愿a大家陪着他们走到那注定的终点。
for /f "eol=为 tokens=2 delims=a " %1 in (t1.txt) do @echo %1
注: 因eol=为,所以将忽略掉所有以"为"打头的行,由于"delims=a",又将每一行以"a"为分割成两部分,同时"tokens=2"即取第二部分.
for /f "eol=为 tokens=1 delims=a " %1 in (t1.txt) do @echo %1
同上,只是最后你取的是以"a"分割的第一部分
for /f "eol=为 tokens=3 delims=a " %1 in (t1.txt) do @echo %1
同上,因t1.txt文件中每行只出现一个"a",所以实际你每行只分隔成两部分,而"tokens=3"去取第3部分,当然不会有显示.
This is the content of the text file t1.txt that I use for testing.
Quote:
In order to hold onto that fleeting feeling,
I have to pick up the pen,
Write down the bits and pieces of this forbidden love.
Maybe dark, maybe cruel, maybe unconventional,
But no one can deny the truest kindness and selflessness of the characters in it.
May everyone accompany them to the destined end.
for /f "eol=为 tokens=2 delims=a " %1 in (t1.txt) do @echo %1
Note: Because eol=为, so all lines starting with "为" will be ignored. Since "delims=a", each line is divided into two parts with "a" as the delimiter, and "tokens=2" means taking the second part.
for /f "eol=为 tokens=1 delims=a " %1 in (t1.txt) do @echo %1
The same as above, only finally you take the first part divided by "a"
for /f "eol=为 tokens=3 delims=a " %1 in (t1.txt) do @echo %1
The same as above, because each line in the t1.txt file has only one "a", so actually each line is only divided into two parts, and "tokens=3" to take the third part, of course, there will be no display.
|
|
2007-1-1 05:19 |
|
|
rochan
初级用户
 
积分 40
发帖 14
注册 2006-12-27
状态 离线
|
『第 4 楼』:
简单点可以这个来回答
使用 LLM 解释/回答一下
楼主 的 字母 a 可以当作是 分隔符 (默认的分隔符 是 空格 或 跳格 )
而那个 tokens= 是指的 列数...
1 就是第一列 ; 2 就是第二列 ; 3 就是第三列...
这样就 好理解多了...
The letter "a" of the thread starter can be regarded as a separator (the default separators are space or tab). And that "tokens=" refers to the number of columns... 1 is the first column; 2 is the second column; 3 is the third column... This is much easier to understand...
|
|
2007-1-4 04:09 |
|
|
rochan
初级用户
 
积分 40
发帖 14
注册 2006-12-27
状态 离线
|
       『第 5 楼』:
使用 LLM 解释/回答一下
为a工了挽住那转逝的感触,
我a不得不提起了笔,
写a下这禁忌之爱的点点滴滴。
或a许阴暗,或许残忍,或许另类,
但a谁也不能否认这其间人物最真的善良与无私。
愿a大家陪着他们走到那注定的终点。
楼主的文字可以用表格形象成 这们..
第一列 第二列 第三列 ......
为 工了挽住那转逝的感触,
我 不得不提起了笔,
写 下这禁忌之爱的点点滴滴。
或 许阴暗,或许残忍,或许另类,
但 谁也不能否认这其间人物最真的善良与无私。
几列都是 以 a 为分隔符 的 ...第三列 是 你写的那 行命令而加上的.
本来 第三列就没有..是空 的..
看了 希望 你明白 ..
愿 大家陪着他们走到那注定的终点。
In order to hold onto those fleeting feelings,
I have to pick up the pen,
Write down the bits and pieces of this forbidden love.
Maybe dark, maybe cruel, maybe unusual,
But no one can deny the truest kindness and selflessness of the characters in it.
I hope everyone can accompany them to the destined end.
The building owner's text can be形象成 this with a table..
First column Second column Third column ......
For To hold onto those fleeting feelings,
I Have to pick up the pen,
Write Down the bits and pieces of this forbidden love.
Or Maybe dark, maybe cruel, maybe unusual,
But No one can deny the truest kindness and selflessness of the characters in it.
Several columns are all ...The third column is added according to the command you wrote.
Originally, the third column was empty..
After reading, I hope you understand ..
Wish Everyone can accompany them to the destined end.
|
|
2007-1-4 04:15 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
           『第 6 楼』:
使用 LLM 解释/回答一下
首先,你要明白,for /f的主要作用是从文本行中提取出特定的字符串。因此,它的每一个循环处理的就是一行文本。既然要从这行文本中提取出特定的字符串,当然,就需要对这一行文本进行分隔,分隔的结果就是将这一行文本分成了若干个字段,例如下面一行文本:
Dog*Cat*Monkey*Donkey
很显然,我们如果以*号作为分隔符,那么这一行就会分为下面四个字段:
Dog Cat Monkey Donkey
ok,现在我们要取出其中的一个或者所有字段进行处理,那么在for /f语句中,字段就是用tokens来代表的。
例如,我们这样写:for /f "tokens=1,2,3,4 delims=*" %i in ('...') do ....
那么以上面一行文本为例:tokens中的1就表示Dog、2表示Cat、依次类推。而在Do语句中,%i 代表tokens(1)的值,即Dog,而%j表示tokens(2)的值即Cat,再次依次类推。
如果我们写成:for /f "tokens=2,4 delims=*" %i in ('...') do ...
那么%i = tokens(2) 即Cat,%j=tokens(4)即Donkey。
另外,在tokens中可以使用*号,表示剩余的全部字段,例如:tokens=1,*
那么这里的*号表示tokens(2,3,4)的全部内容,也就是%j = Cat Monkey Donkey。
关于tokens大致就是这些东西了,以上的描述中可能有不准确的地方,以实践为准。
First of all, you need to understand that the main function of for /f is to extract specific strings from text lines. Therefore, each loop of it processes one line of text. Since you need to extract specific strings from this line of text, you naturally need to separate this line of text. The result of the separation is to divide this line of text into several fields. For example, the following line of text:
Dog*Cat*Monkey*Donkey
Obviously, if we use the * sign as the delimiter, then this line will be divided into the following four fields:
Dog Cat Monkey Donkey
OK, now we need to take out one or all fields for processing. Then in the for /f statement, the fields are represented by tokens.
For example, we write like this: for /f "tokens=1,2,3,4 delims=*" %i in ('...') do ....
Taking the above line of text as an example: 1 in tokens means Dog, 2 means Cat, and so on. In the Do statement, %i represents the value of tokens(1), that is, Dog, and %j represents the value of tokens(2), that is, Cat, and so on.
If we write: for /f "tokens=2,4 delims=*" %i in ('...') do ...
Then %i = tokens(2) which is Cat, and %j = tokens(4) which is Donkey.
In addition, the * sign can be used in tokens to represent all remaining fields, for example: tokens=1,*
Then this * sign means all the contents of tokens(2,3,4), that is, %j = Cat Monkey Donkey.
That's roughly all about tokens. There may be inaccuracies in the above description, and it is subject to practice.
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2007-1-5 23:01 |
|
|
bigcarp
初级用户
 
积分 21
发帖 6
注册 2006-12-17
状态 离线
|
|
2007-1-15 05:00 |
|
|
oilio
高级用户
    前进者
积分 641
发帖 303
注册 2007-1-10
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
嗯,看完了,又加深了一些印像,for是要好好学的。
Well, after reading, it has deepened some impressions. for needs to be studied well.
|

我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿! |
|
2007-1-16 09:53 |
|
|
tyrbl
初级用户
 
积分 22
发帖 9
注册 2006-7-3
状态 离线
|
|
2007-1-22 11:56 |
|
|
hangyug
初级用户
 
积分 99
发帖 43
注册 2007-1-12
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
本来我也是不懂的,看完6楼的解释真是茅塞顿开啊,啥也不说了,回去接着学。。。
Originally, I also didn't understand. After reading the explanation on floor 6, I suddenly became enlightened. No need to say anything, I'll go back and continue studying...
|
|
2007-1-22 22:42 |
|
|
kevinye
新手上路

积分 10
发帖 4
注册 2007-3-7
状态 离线
|
|
2007-3-9 08:31 |
|
|
nzisisco
初级用户
 
积分 107
发帖 47
注册 2007-3-3
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
6楼讲的好 明白了很多``````信心又来了
What the 6th floor said is great. I've understood a lot... and I'm full of confidence again.
|
|
2007-3-10 16:34 |
|
|
zhxy9804
中级用户
  
积分 208
发帖 97
注册 2006-12-28
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
给他加分啊 6楼 这样光说好有什么用 付出点实际行动来!
Give him some points. 6th floor. Just saying it's good is not enough. Take some practical actions!
|
|
2007-3-11 12:16 |
|
|
hy433124shc
中级用户
  
积分 201
发帖 75
注册 2007-2-24
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
6楼的通俗易懂!
说得明明白白.清清楚楚!
我给加2分!
The explanation on the 6th floor is easy to understand!
It's said clearly and explicitly!
I'll add 2 points!
|
|
2007-3-12 08:51 |
|
|
tianlijian
初级用户
 
积分 120
发帖 45
注册 2007-3-13
状态 离线
|
|
2007-3-13 11:58 |
|
|