简体   繁体   English

在带引号的逗号中使用Windows命令行FOR命令

[英]Using Windows Command Line FOR command with quoted commas

I am trying to process a file containing values formatted like this: 我正在尝试处理包含如下格式的值的文件:

 Token1    Token2            Token3    Token4
"ValueA", "ValueB, ValueC", "ValueD", "Value E"

(Headers added by me for clarity). (为清楚起见,我添加了标题)。 I just want tokens 2 and 4. 我只想要代币2和4。

The following command: 以下命令:

for /f "tokens=2,4 delims=," %a in (TestSource.txt) do @echo %a %b

Does work, but of course when Token2 contains a comma (as it does in the above example), it gets broken at that comma since the FOR command doesn't seem to know any better. 确实有效,但是当Token2包含逗号时(如上例所示),由于FOR命令似乎并不了解逗号,因此它在该逗号处破了。

Are there any known workarounds to this limitation - I did quite a bit of looking into it and saw a "usebackq" command, but I don't think this is what I need. 有没有已知的解决方法来解决此限制-我做了很多研究,看到了“ usebackq”命令,但是我认为这不是我所需要的。

Thanks! 谢谢!

for /f tokens^=3^,7^ delims^=^" %f in (data.csv) do echo "%f", "%g"

There are two ways to include a "options" string for the for command. 有两种方法可以为for命令包括“选项”字符串。 As it can include spaces and special characters, the first is the usual quoted string. 由于可以包含空格和特殊字符,因此第一个是通常带引号的字符串。 But is some cases, it is not sufficient. 但是在某些情况下,这还不够。

The second way is shown in the included code: escape all the separators in the options string. 包含的代码中显示了第二种方法:转义选项字符串中的所有分隔符。

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

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