在`for / F`的`delims`选项中转义双引号 [英] Escaping double-quote in `delims` option of `for /F`

查看:766
本文介绍了在`for / F`的`delims`选项中转义双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个麻烦的批处理脚本,需要解析一个配置文件中的一个值变量。

I'm having a bit of trouble with a batch script which needs to parse a value out of an config file into a variable.

适当匿名,相关文件的行看起来像

Suitably anonymised, the relevant line of the file looks like

<?define ProductShortName="Foo" ?>

我想设置一个变量为 Foo 。字符串 ProductShortName 是独一无二的,可以获取 findstr 的行,但是我必须提取该值。对于/ F ,正确的方法似乎是,但是以下所有错误都出现错误:

I want to set a variable to Foo. The string ProductShortName is unique enough to get the line with findstr, but then I have to extract the value. The correct approach seems to be for /F, but all of the following give errors:

for /F "delims=^" usebackq" %%G in (`findstr /L "ProductShortName" "%~dp0Installer\Branding.wxi"`)
for /F "delims="" usebackq" %%G in (`findstr /L "ProductShortName" "%~dp0Installer\Branding.wxi"`)
for /F "delims=\" usebackq" %%G in (`findstr /L "ProductShortName" "%~dp0Installer\Branding.wxi"`)
for /F 'delims=^" usebackq' %%G in (`findstr /L "ProductShortName" "%~dp0Installer\Branding.wxi"`)
for /F 'delims=" usebackq' %%G in (`findstr /L "ProductShortName" "%~dp0Installer\Branding.wxi"`)
for /F "delims=" usebackq" %%G in (`findstr /L "ProductShortName" "%~dp0Installer\Branding.wxi"`)

大多沿着

usebackq" %G in (`findstr /L "ProductShortName" "C:\foo\bar\Installer\Branding.wxi"`) was unexpected at this time.

正确的方法是将其转义为

What's the correct way of escaping it to split the string on "?

推荐答案

您可以使用双引号作为分隔符,其语法如下:

You can use the double quotation mark as a delimiter with syntax like:

FOR /F delims^=^"^ tokens^=2 %G IN ('echo I "want" a "pony"') DO @ECHO %G

在命令行上运行时,使用令牌^ = 2 应该给你想要,而4个令牌让你成为一个小马。

When run on the command line, using tokens^=2 should give you want, and 4 tokens gets you a pony.

技巧对你的原始问题,这应该在您的批处理文件中工作:

Applying the technique to your original question, this should work in your batch file:

FOR /F delims^=^"^ tokens^=2 %%G IN ('FINDSTR /L "ProductShortName" "data.txt"')

详细信息

我不是命令行解析器的怪癖,但它可能有助于想到通常的de lims = blah tokens = blah作为传入FOR的单个组合参数。在 delims ^ = blah ^  tokens ^ = blah 中的插入符号逃避技巧绕过了将序列作为单个参数处理时需要封闭引号。我在这里使用了一些创新的比喻,效果在整个shell中并不普遍。例如。你不能做 dir C:^ \Program ^ Files (这是有道理的,因为 ^ 是一个有效的文件名字符)。

I'm no expert on quirks of the command line parser, but it may help to think of the usual "delims=blah tokens=blah" as a single, combined argument passed to FOR. The caret escaping trick in delims^=blah^ tokens^=blah bypasses the need for enclosing quotes while still treating the sequence as a single argument. I've used a bit of creative analogy here, and the effect isn't universal across the shell. E.g. you can't do dir C:^\Program^ Files (which makes sense since ^ is a valid filename character).

测试用例

有了足够的转义,您可以快速检查您在命令行上的原始示例:

With sufficient escaping, you can quickly check your original sample on the command line:

FOR /F delims^=^"^ tokens^=2 %G IN ('echo ^^^<?define ProductShortName="Foo" ?^^^>') DO @ECHO %G

其他玩这个的人可能想创建一个文件 testcases.txt

Others playing with this may want to create a file testcases.txt:

blah blah "red"
     blah "green" blah
How about a "white" "unicorn"?

并运行如下:

FOR /F delims^=^"^ tokens^=2 %G IN (testcases.txt) DO @ECHO %G

以检查各种输入的结果。在这种情况下,应该产生:

to check results for a variety of inputs. In this case it should yield:

red
green
white

最后一个例子:

FOR /F delims^=^"^ tokens^=2 %G IN ('FINDSTR /L "unicorn" "testcases.txt"') ^
DO @ECHO The unicorn is %G.

最后,请注意,我的测试是在Windows Server 2003上完成的。

Finally, note my testing for this was done on Windows Server 2003.

这篇关于在`for / F`的`delims`选项中转义双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆