Windows 批处理 FOR 命令制表符分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13734726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows batch FOR command tab delimiter
提问by jterrace
I'm trying to use the Windows batch FOR command.
我正在尝试使用 Windows 批处理FOR 命令。
How do I specify a TAB character as a delimiter? The default is space and tab, but I want tab only.
如何指定 TAB 字符作为分隔符?默认是空格和制表符,但我只想要制表符。
I've tried this, but it doesn't work:
我试过这个,但它不起作用:
FOR /F "tokens=3 delims=^T" %i in (test.txt) do @echo %i
It seems to be splitting on the T character instead of a tab. I've also tried a literal pasted into the command prompt, but I get a syntax error.
它似乎在 T 字符而不是制表符上拆分。我还尝试将文字粘贴到命令提示符中,但出现语法错误。
回答by dbenham
You need a literal tab character in the quoted options string. The literal TAB works both in a batch file and on the command line.
您需要在带引号的选项字符串中使用文字制表符。文字 TAB 在批处理文件和命令行中均可使用。
If you are attempting to type a TAB into the command line, then file name completion may be thwarting your efforts. You can disable name completion by using CMD /F:OFF
. You will then be able to enter a literal TAB character.
如果您尝试在命令行中键入 TAB,则文件名补全可能会阻碍您的努力。您可以使用CMD /F:OFF
. 然后,您将能够输入文字 TAB 字符。
You can also run into problems trying to enter a TAB into a batch script depending on the text editor that you use. Some programmer's editors have settings that automatically convert TABs into spaces. You will need to make sure your editor is not doing that.
根据您使用的文本编辑器,您还可能会在尝试将 TAB 输入批处理脚本时遇到问题。一些程序员的编辑器具有自动将 TAB 转换为空格的设置。您需要确保您的编辑器没有这样做。
So you just need to change your "tokens=3 delims=^T"
string to use a TAB literal instead of ^T
所以你只需要改变你的"tokens=3 delims=^T"
字符串来使用 TAB 文字而不是^T
回答by djb
Ngaaa.. talk about an afternoon headache...
Ngaaa.. 说说下午的头痛...
I had the same problem today... I needed to parse lines with FOR /F where each line was a number of sentences (each with embedded spaces, maybe) with each sentence delimited by TABs.
我今天遇到了同样的问题......我需要用 FOR /F 解析行,其中每行是许多句子(每个都有嵌入的空格,也许),每个句子由制表符分隔。
My problem is that my company coding standards (even for BATCH files) prohibits actual TAB characters from being in the source code (our editors prohibit putting TABs in, and our Version ctrl tools disallow any CheckIn of any text file with TABs or WTR [white-to-the-right].)
我的问题是我公司的编码标准(即使对于 BATCH 文件)禁止在源代码中使用实际的 TAB 字符(我们的编辑器禁止将 TAB 放入,我们的版本 ctrl 工具不允许任何带有 TAB 或 WTR 的文本文件的任何签入 [白色-向右]。)
So, this is a hack, but it works on XP (hangs head... deep shame...)
所以,这是一个黑客,但它适用于 XP(悬着头......深深的耻辱......)
SET REG_CPU="HKLM\Hardware\Description\System\CentralProcessor" IDENTIFIER<tab>REG_SZ<tab>x86 Family 6 Model 37 Stepping 2"
"
REG.EXE QUERY %REG_CPU% /V IDENTIFIER | FIND "REG_SZ" > RegCPUid.Tmp
for /F "tokens=2 delims=rR" %%i in (RegCPUid.Tmp) do SET TAB=%%~i
This works on XP but I havent checked Win7 or Win7[WoW64].
这适用于 XP,但我还没有检查过 Win7 或 Win7[WoW64]。
The temporary contents of RegCPUid.Tmp is ONE LINE, which looks like
RegCPUid.Tmp 的临时内容是 ONE LINE,看起来像
for /F "tokens=1-3* delims=%TAB%" %%i in (Sentences.txt) do ECHO The THIRD sentence is %%k
The FOR command simply plucks out the lonely TABbetween the two "R"s of IDENTIFIER and REG_SZ and sets the environment variable TAB to it.
FOR 命令只是从IDENTIFIER 和 REG_SZ 的两个“R”之间取出单独的TAB并将环境变量 TAB 设置为它。
Then my script can do all the FOR /F looping it wants with ONLY the tab character as a delimiter (and NOT a space) by...
然后我的脚本可以只使用制表符作为分隔符(而不是空格)来完成它想要的所有 FOR /F 循环...
##代码##All of this allows me to NOT have any hardcoded TAB characters in the BATCH file.
所有这些都允许我在 BATCH 文件中没有任何硬编码的 TAB 字符。
-dave
-戴夫
回答by jterrace
It appears that a literal tab character does work, but you have to starts the cmd
process with /F:ON
. Without that, even pasting in a tab character doesn't work.
看来,文字制表符的工作,但你必须在开始cmd
与过程/F:ON
。没有它,即使粘贴制表符也不起作用。
回答by calmond
According to this website http://ss64.com/nt/for_f.html, the default delimiter is a tab, so try it without the delims parameter.
根据这个网站http://ss64.com/nt/for_f.html,默认分隔符是一个制表符,所以试试不带 delims 参数。