简体   繁体   English

Windows批处理FOR命令选项卡分隔符

[英]Windows batch FOR command tab delimiter

I'm trying to use the Windows batch FOR command . 我正在尝试使用Windows批处理FOR命令

How do I specify a TAB character as a delimiter? 如何将TAB字符指定为分隔符? The default is space and tab, but I want tab only. 默认为空格和制表符,但我只想要制表符。

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. 它似乎是在T字符而不是标签上分裂。 I've also tried a literal pasted into the command prompt, but I get a syntax error. 我还尝试了一个粘贴到命令提示符的文字,但是我收到了语法错误。

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. 如果您尝试在命令行中键入TAB,则文件名完成可能会阻碍您的工作。 You can disable name completion by using CMD /F:OFF . 您可以使用CMD /F:OFF禁用名称完成。 You will then be able to enter a literal TAB character. 然后,您就可以输入文字TAB字符。

You can also run into problems trying to enter a TAB into a batch script depending on the text editor that you use. 根据您使用的文本编辑器,尝试将TAB输入批处理脚本时也会遇到问题。 Some programmer's editors have settings that automatically convert TABs into spaces. 一些程序员的编辑器具有自动将TAB转换为空格的设置。 You will need to make sure your editor is not doing that. 您需要确保您的编辑器没有这样做。

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

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解析行,其中每行是多个句子(每个都有嵌入的空格),每个句子由TAB分隔。

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放入,我们的版本控制工具禁止任何带有TAB或WTR的文本文件的CheckIn [白色] -在右边]。)

So, this is a hack, but it works on XP (hangs head... deep shame...) 所以,这是一个黑客,但它适用于XP(挂头......深深的羞耻......)

SET REG_CPU="HKLM\Hardware\Description\System\CentralProcessor\0"
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,看起来像

"    IDENTIFIER<tab>REG_SZ<tab>x86 Family 6 Model 37 Stepping 2"    

The FOR command simply plucks out the lonely TAB between 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循环,只需将制表符作为分隔符(而不是空格)...

for /F "tokens=1-3* delims=%TAB%" %%i in (Sentences.txt) do ECHO The THIRD sentence is %%k

All of this allows me to NOT have any hardcoded TAB characters in the BATCH file. 所有这些都允许我在BATCH文件中没有任何硬编码的TAB字符。

-dave 戴夫

It appears that a literal tab character does work, but you have to starts the cmd process with /F:ON . 看起来文字制表符确实起作用,但您必须使用/F:ON启动cmd进程。 Without that, even pasting in a tab character doesn't work. 没有它,即使粘贴在制表符中也不起作用。

根据这个网站http://ss64.com/nt/for_f.html ,默认分隔符是一个选项卡,所以在没有delims参数的情况下尝试它。

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

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