gpt4 book ai didi

batch-file - for 循环中的管道中断双引号变量

转载 作者:行者123 更新时间:2023-12-04 02:20:42 25 4
gpt4 key购买 nike

情况:使用批处理脚本从 JSON 中检索某些值。
我有以下批处理脚本:

@ECHO off

ECHO Enter npo.nl program-url :
SET url=
SET /P url=
:: http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040 for example
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=6 delims=/" %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('curl.exe -s http://e.omroep.nl/metadata/aflevering/%%A ^| jq.exe -R -r -s ".[1+index(\"^(\"): rindex(\"^)\")]"') DO (
FOR /F "delims=" %%C IN ('ECHO %%B ^| jq.exe -r .start') DO SET ss=%%C
FOR /F "delims=" %%C IN ('ECHO %%B ^| jq.exe -r .eind') DO SET to=%%C
FOR /F "delims=" %%C IN ('ECHO %%B ^| jq.exe -r .tijdsduur') DO SET t=%%C
ECHO Start: !ss!
ECHO End: !to!
ECHO Duration: !t!
)
)
ENDLOCAL
PAUSE

它有什么作用?
输入 npo.nl 程序 url 后,第一个 for 循环将 url 剥离到 prid:POMS_VPRO_850040。在第二个 for 循环中 curl.exe 检索 JSON...:

parseMetadata({"STATUS":"OK","VERSION":"1.11.12","prid":"VPWON_1232766","titel":"Schuim & As","aflevering_titel":"","info":"Schuim & As met Jelle Brandt Corstius","ratio":"16:9","medium":"tv","gidsdatum":"2015-05-03","tijdsduur":"00:05:27","start":"00:23:13","eind":"00:28:40","url":"","webcast":1,"images":[{"size":"640x480","ratio":"4:3","url":"http:\/\/images.poms.omroep.nl\/image\/sx480\/c640x480\/606030.jpg"},{"size":"720x405","ratio":"16:9","url":"http:\/\/images.poms.omroep.nl\/image\/sx405\/c720x405\/606030.jpg"}],"omroepen":[{"naam":"VPRO"}],"pubopties":["adaptive","h264_bb","h264_sb","h264_std"],"tt888":"ja","serie":{"srid":"VPWON_1232748","serie_titel":"Buitenhof","serie_url":null},"sitestat":{"baseurl":"http:\/\/b.scorecardresearch.com\/p?c1=2&c2=17827132&ns_site=po-totaal","programurl":"uitzendinggemist.publiekeomroep.ondemand.tv.buitenhof.20150503","programurlpost":"category=uitzendinggemist&thema=informatief&po_source=video","baseurl_subtitle":"http:\/\/nl.sitestat.com\/klo\/po\/s","subtitleurl":"uitzendinggemist.publiekeomroep.ondemand.tv.player.tt888.buitenhof","subtitleurlpost":"category=uitzendinggemist&po_source=video&po_sitetype=webonly"},"reclame":"http:\/\/pubads.g.doubleclick.net\/gampad\/ads?_cookie_&impl=s&gdfp_req=1&env=vp&output=xml_vast2&unviewed_position_start=1&sz=_sz_&correlator=_correlator_&iu=\/9233\/_site_\/buitenhof&url=_url_&cust_params=genre%3Dinformatief%2Cnieuws%2Factualiteiten%26dur%3D3284%26prid%3DVPWON_1232766%26srid%3DVPWON_1232748%26player%3D_player_","streamSense":{"episode":"buitenhof","program":"buitenhof","station":"nederland_1","sitestatname":"uitzendinggemist.publiekeomroep.ondemand.tv.buitenhof.20150503","sko":"TRUE","sko_dt":"20150503","sko_pr":"buitenhof","sko_stid":"1","sko_ty":"tv.seg","sko_prid":"vpwon1232766","sko_t":"1210","sko_cl":"3284"}})
//epc

...并通过管道将其发送到 jq.exe 以删除非 JSON 数据 parseMetadata( and )//epc 并保持单行不变。这是出于 2 个原因:1) 存在非 JSON 数据时我们无法处理任何内容,以及 2) for 循环一次仅处理 1 行。
随后的 jq.exe 检索不带双引号的指定对象的值。
只要 curl.exe 和 jq.exe 与批处理脚本在同一目录中,或者在 %path% 变量中,就可以正常工作:

Start:     00:23:13
End: 00:28:40
Duration: 00:05:27

现在我想从另一个 map 调用 curl.exe 和 jq.exe。一个里面有空格:

SET curl="C:\map with spaces\curl.exe"
SET jq="C:\map with spaces\jq.exe"

@ECHO off

ECHO Enter npo.nl program-url :
SET url=
SET /P url=
:: http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040 for example
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=6 delims=/" %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('%curl% -s http://e.omroep.nl/metadata/aflevering/%%A ^| %jq% -R -r -s ".[1+index(\"^(\"): rindex(\"^)\")]"') DO (
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% -r .start') DO SET ss=%%C
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% -r .eind') DO SET to=%%C
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% -r .tijdsduur') DO SET t=%%C
ECHO Start: !ss!
ECHO End: !to!
ECHO Duration: !t!
)
)
ENDLOCAL
PAUSE

对于第二个 for 循环,这会导致问题:

'C:\map' is not recognized as an internal or external command,
operable program or batch file.

虽然 'ECHO %%X ^| %jq%' 确实有效,看起来 '%curl% ^| %jq%' 没有。因此,由于某种原因,一旦管道中的 2 个变量被解析,事情就会出错。

那么,没有更多的管道了:

SET curl="C:\map with spaces\curl.exe"
SET jq="C:\map with spaces\jq.exe"

@ECHO off

ECHO Enter npo.nl program-url :
SET url=
SET /P url=
:: http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040 for example
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=6 delims=/" %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('%curl% -s http://e.omroep.nl/metadata/aflevering/%%A') DO (
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% -R -r -s ".[1+index(\"^(\"): rindex(\"^)\")]"') DO (
FOR /F "delims=" %%D IN ('ECHO %%C ^| %jq% -r .start') DO SET ss=%%D
FOR /F "delims=" %%D IN ('ECHO %%C ^| %jq% -r .eind') DO SET to=%%D
FOR /F "delims=" %%D IN ('ECHO %%C ^| %jq% -r .tijdsduur') DO SET t=%%D
ECHO Start: !ss!
ECHO End: !to!
ECHO Duration: !t!
)
)
)
ENDLOCAL
PAUSE

现在 curl.exe 和 jq.exe 各自在一个 for 循环中。起初这似乎工作正常。回显了 3 个值,但随后出现问题:

parse error: Invalid numeric literal at line 1, column 5

parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 5
Start: 00:23:13
End: 00:28:40
Duration: 00:05:27

就像我之前说的; for 循环一次只解析和处理 1 行。第 2 行的非 JSON 数据 //epc 导致 for 循环重新开始,如您所见,这是非常错误的。这就是上面代码中 curl 和 jq 之间的管道的原因。输出 1 行以进行处理。遗憾的是,这也没有用……唉。

当然,当 curl 和 jq 仍在包含空格的映射中时,使用临时文件是最后的手段,但我更喜欢使用变量,因此我正在尝试解决管道问题。例如,我尝试在 for 循环中使用反引号而不是单引号在 for 循环中使用“usebackq”,但无济于事。
到目前为止,我还没有找到解决方案。有没有人对此行为有解释以及如何解决?

最佳答案

感谢 Dave Benham's answer on a related issue,我找到了解决方案!
它似乎是 WinXP 中的一个特定的 FOR/F 错误,你猜怎么着,我仍然在 WinXP 上。为了解决主要问题,curl-pipe-jq-for-loop,我不得不在整个命令之后将 ^" 放在 前面。因此整个批处理脚本,我还进一步改进了:

@ECHO off
CLS

:: NPO JSON-extractor geschreven door Reino Wijnsma, 2015 (reino@degeelebosch.nl)

SET batchname=NPO JSON-extractor
SET version=1.1
TITLE %batchname% %version%

SET curl="C:\map with spaces\curl.exe"
SET jq="C:\map with spaces\jq-1.5rc1.exe"

:Check
IF EXIST %curl% (
IF EXIST %jq% (
GOTO Input
) ELSE (
ECHO 'jq.exe' niet gevonden.
ECHO.
PAUSE
GOTO :eof
)
GOTO Input
) ELSE (
ECHO 'curl.exe' niet gevonden.
ECHO.
PAUSE
GOTO :eof
)

:Input
ECHO Voer npo.nl programmalink in :
SET url=
SET /P url=
:: http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040 bijvoorbeeld
IF "%url%"=="" GOTO :eof

SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('^"%curl% -s http://e.omroep.nl/metadata/aflevering/%%~nxA ^| %jq% -R -r -s ".[1+index(\"(\"): rindex(\"^)\")]"^"') DO (
ECHO.
ECHO JSON:
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% .') DO ECHO %%C
ECHO.
FOR /F "tokens=1-3" %%C IN ('ECHO %%B ^| %jq% -r "[.tijdsduur,.start,.eind] | @tsv"') DO (
ECHO Tijdsduur: %%C
IF NOT "%%D"=="" (
SET ss=%%D
SET to=%%E
SET /A "_ss=((1!ss:~0,2!-100)*3600)+((1!ss:~3,2!-100)*60)+(1!ss:~6,2!-100)"
SET /A "_to=((1!to:~0,2!-100)*3600)+((1!to:~3,2!-100)*60)+(1!to:~6,2!-100)"
ECHO Start: %%D (!_ss!s^)
ECHO Einde: %%E (!_to!s^)
)
)
)
)
ECHO.
ENDLOCAL
GOTO Input


供日后引用的重要说明:

jq-syntax:           jq -R -r -s  '.[1+index("("):   rindex(")")]'
cmd-shell: jq -R -r -s ".[1+index(\"(\"): rindex(\")\")]"
for-loop: 'jq -R -r -s ".[1+index(\"(\"): rindex(\"^)\")]"'
for-loop (path): '^"%jq% -R -r -s ".[1+index(\"(\"): rindex(\"^)\")]"^"'

- 对于 cmd-shell,您必须使用换行 \ 来转义双引号。
- 虽然右括号中的 2 是 jq 语法的一部分,但双引号之间的 1 不是。因此,当在 for 循环中时,要防止此 for 循环关闭,您必须使用 ^ 转义此循环。
- 当 jq 的可执行文件路径放在带双引号的变量中时,为了避免 WinXP 错误,您还必须将整个命令放在 ^" 之间,因为括号现在被视为特殊字符! 此解决方法与 Vista 及更高版本兼容。(另请参阅 DosTips.com )

关于batch-file - for 循环中的管道中断双引号变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30121075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com