简体   繁体   English

是否在功能上等效于使用“ tokens = *”或“ delims =”调用FOR?

[英]Is calling FOR with “tokens=*” or “delims=” functionally equivalent?

Consider the following two loops: 考虑以下两个循环:

for /f "tokens=*" %%a in ('dir /b %TEMP%') do (
   echo %%a
)

and

for /f "delims=" %%a in ('dir /b %TEMP%') do (
   echo %%a
)

If my desired result is to provide the variable %%a with the contents of the entire line being evaluated, are the options tokens=* and delims= functionally equivalent? 如果我想要的结果是为变量%%a提供要评估的整行内容,选项tokens=*delims=功能上是否等效?

Are there any situations where I might get a different output with one or the other given a particular input? 在任何情况下,如果给定特定输入,一个或另一个我可能会得到不同的输出?

I would like to know if these two options should always be used in combination to guarantee coverage of all potential cases or if specifying both is redundant. 我想知道这两个选项是否应始终结合使用以保证涵盖所有潜在情况,或者同时指定这两个选项是否多余。

Yes, tokens=* and delims= are different: 是的, tokens=*delims=是不同的:

  • delims= returns the whole line unedited; delims=返回未编辑的整个行;
  • tokens=* returns the line with any leading delimiters ( SPACE and TAB as per default) removed; tokens=*返回删除任何前导分隔符(默认情况下为SPACETAB )的行;
  • tokens=* delims= behaves exactly the same way as delims= ; tokens=* delims=行为完全相同的方式, delims= ;

Note that empty lines are always skipped. 注意,总是跳过空行。 Also lines that begin with ; 也以开头的行; are ignored as that character is the default eol . 该字符是默认的eol ,将被忽略。

If tokens=* is specified and a line contains delimiters only, the for /F loop iterates and the meta variable returns an empty string. 如果指定了tokens=*并且一行仅包含定界符,则for /F循环将迭代,并且meta变量将返回一个空字符串。 As soon as any token number is provided (like tokens=3 , tokens=1,3 , tokens=2-3 , tokens=2* , etc.), delimiter-only lines are skipped. 一旦提供了任何令牌编号(如tokens=3tokens=1,3tokens=2-3tokens=2*等),将跳过仅分隔符的行。
However, a line that contains one or more delimiters plus the eol character plus an arbitrary string are ignored even when tokens=* is provided. 但是,即使提供了tokens=*也将忽略包含一个或多个定界符以及eol字符以及任意字符串的行。


For evidence I did some tests, using the following text file sample.txt (note that the 2 nd line is empty, the 4 th line contains four SPACEs ; click on the edit button below this answer and view the raw text): 对于证据我做了一些测试,使用下面的文本文件sample.txt (注意, 第二行是空的, 4行中包含四个空格 ;在点击编辑这个答案下面的按钮,查看原始文本):

 text_without_delimiters text with delimiters text with leading and trailing delimiters ; comment text ; comment text with leading delimiters text plus ; comment text 

And here is what I did on the console together with the respective return strings: 这是我在控制台上所做的以及相应的返回字符串:

>>> for /F %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text"
"text"
"text"

>>> for /F "tokens=*" %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text with delimiters"
""
"text with leading and trailing delimiters  "
"text plus ; comment text"

>>> for /F "delims=" %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text with delimiters"
"    "
"  text with leading and trailing delimiters  "
"  ; comment text with leading delimiters"
"text plus ; comment text"

>>> for /F "tokens=* delims=" %I in (sample.txt) do @echo "%I"
"text_without_delimiters"
"text with delimiters"
"    "
"  text with leading and trailing delimiters  "
"  ; comment text with leading delimiters"
"text plus ; comment text"

>>> for /F "tokens=3" %I in (sample.txt) do @echo "%I"
"delimiters"
"leading"
";"

>>> for /F "tokens=1,3" %I in (sample.txt) do @echo "%I"    "%J"
"text_without_delimiters"    ""
"text"    "delimiters"
"text"    "leading"
"text"    ";"

>>> for /F "tokens=2-3" %I in (sample.txt) do @echo "%I"    "%J"
"with"    "delimiters"
"with"    "leading"
"plus"    ";"

>>> for /F "tokens=2*" %I in (sample.txt) do @echo "%I"    "%J"
"with"    "delimiters"
"with"    "leading and trailing delimiters  "
"plus"    "; comment text"

So the only really strange and unexpected output is the line "" with the tokens=* option alone. 因此,唯一真正奇怪和意外的输出是仅带有tokens=*选项的""行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM