在`的delims`选择逃避双引号'为/ F` [英] Escaping double-quote in `delims` option of `for /F`

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

问题描述

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

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" ?>

我要设置为的变量。字符串 ProductShortName 是唯一的,足以获得与 FINDSTR 行,但后来我不得不提取价值。正确的做法似乎是 FOR / 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.

什么是逃避它分裂的字符串的正确方法

推荐答案

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

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"')

详细信息

我在命令行分析器的怪癖专家,但它可以帮助想到平时的delims =等等令牌=嗒嗒作为传递给单个组合的说法。在尖逃逸招delims ^ =胡说^&NBSP;令牌^ =等等绕过围住报价,同时还处理的顺序作为单个参数的需要。我用了一个有点创意的比喻在这里,效果是不能跨越的外壳通用。例如。你不能做 DIR C:^ \\ ^程序文件(这是有道理的,因为 ^ 是一个有效的文件名字符)。

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

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.

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

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