gpt4 book ai didi

Windows 批处理文件 - 在变量上查找字符串,查找包含百分比字符和单引号的字符串

转载 作者:可可西里 更新时间:2023-11-01 10:34:42 25 4
gpt4 key购买 nike

在 Windows 批处理文件中,我正在检查作为输入给出的文本文件的内容 - 假设包含此文本的单行文本文件 (test.txt):

"bla //ID_SCEN='%3' blabla"

其中一项检查包括使用 findstr 在行中查找特定字符串 (//ID_SCEN='%3')。测试代码如下(test.bat)。

@echo off
set myLine=
(
set /p myLine=
)<%1
echo Just found the following line in the input file:
echo %myLine%
echo %myLine% | findstr /C:"//ID_SCEN=^'%%3^'" 1>nul
if errorlevel 1 (
echo The line %myLine% does not contain //ID_SCEN='%%3'
echo Too bad, it is compulsory ... I quit
GOTO:EOF
) else (
echo Found expected stuff
)

现在 test.bat test.txt 的输出是:

Just found the following line in the input file:
"bla //ID_SCEN='%3' blabla"
The line "bla //ID_SCEN='%3' blabla" does not contain //ID_SCEN='%3'
Too bad, it is compulsory ... I quit

有人可以帮忙吗?它可能与转义字符有关 - 根据论坛帖子尝试了各种方法但尚未成功......

接受的答案:

替换搜索字符串中单引号的^转义:"//ID_SCEN=^'%%3^'""//ID_SCEN ='%%3'" ... 成功了

最佳

最佳答案

FINDSTR 中的单引号不需要转义。改变...

echo %myLine% | findstr /C:"//ID_SCEN=^'%%3^'" 1>nul

...到...

echo %myLine% | findstr /C:"//ID_SCEN='%%3'" 1>nul

...并且有效。

但是,请注意 FINDSTR 本身就是一团糟。它包含许多奇怪的边缘案例和未记录的行为。看看dbenhamexhaustive investigation on the matter :)

关于Windows 批处理文件 - 在变量上查找字符串,查找包含百分比字符和单引号的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33565709/

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