首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将for循环中的特殊字符从文件解析为变量

将for循环中的特殊字符从文件解析为变量
EN

Stack Overflow用户
提问于 2017-10-24 08:16:30
回答 2查看 95关注 0票数 0

我试图使用批处理文件从文件中设置变量(例如,file散列是变量名)。唉,我被困在这两个问题上:

  1. 当它包含特殊字符时,特别是我使用enableDelayedExpansion时的感叹号。
  2. 在这样做时,我还需要将&更改为&。

我尝试过从不同来源提供的几种解决方案,但没有一种适合我的要求。

以下是我的当前代码:

代码语言:javascript
复制
@echo off
set grep=binaries\grep.exe
set fileindex=binaries\index.htm
set count=0
for /f "tokens=* delims=" %%a in (crc.dat) do ( 
set count=!count! + 1
set filehash=%%a
%grep% --ignore-case -w "!filehash!" %fileindex%>> list.dat
)

还有一个crc.dat文件内部的例子:

代码语言:javascript
复制
Kabooom! Kablooey!
Kaboom & Kablooey

以及在list.dat中使用当前代码的结果:

代码语言:javascript
复制
Kabooom Kablooey
Kaboom & Kablooey

我在list.dat中期望的结果是:

代码语言:javascript
复制
Kabooom! Kablooey!
Kaboom & Kablooey

我希望我能正确地表达我的问题,并提前感谢你!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-24 14:27:49

若要将&替换为&input of grep中,请使用以下命令:

代码语言:javascript
复制
@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "grep=binaries\grep.exe"
set "fileindex=binaries\index.htm"
set "file=crc.dat"
set "list=list.dat"

rem // Initialise counter:
set /A "count=0"
> "%list%" (
    for /F "usebackq delims=" %%a in ("%file%") do (
        rem // Increment counter:
        set /A "count+=1"
        rem // Assign line string to variable:
        set "lineitem=%%a"
        rem // Toggle delayed expansion:
        setlocal EnableDelayedExpansion
        rem // Do sub-string replacement:
        set "lineitem=!lineitem:&=&!"
        rem // Execute `grep` command line:
        "!grep!" --ignore-case -w "!lineitem!" "!fileindex!"
        endlocal
    )
)
rem // Return counter:
echo/%count%

endlocal

若要将&替换为&output of grep中,请使用以下命令:

代码语言:javascript
复制
@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "grep=binaries\grep.exe"
set "fileindex=binaries\index.htm"
set "file=crc.dat"
set "list=list.dat"

rem // Initialise counter:
set /A "count=0"
> "%list%" (
    for /F "usebackq delims=" %%a in ("%file%") do (
        rem // Increment counter:
        set /A "count+=1"
        rem // Execute `grep` command line and capture its output by `for /F`:
        for /F "delims=" %%b in ('^""%grep%" --ignore-case -w "%%a" "%fileindex%"^"') do (
            rem // Assign `grep` output to variable:
            set "lineitem=%%b"
            rem // Toggle delayed expansion:
            setlocal EnableDelayedExpansion
            rem // Do sub-string replacement:
            set "lineitem=!lineitem:&=&!"
            rem // Return modified string:
            echo(!lineitem!
            endlocal
        )
    )
)
rem // Return counter:
echo/%count%

endlocal

通常,若要处理带有感叹号的字符串,必须切换延迟展开,以便正常的%-expanded变量和for变量引用(如%%a )在禁用延迟展开时展开。

票数 1
EN

Stack Overflow用户

发布于 2017-10-24 08:21:48

这是一个delayedExpansion-free的解决方案,可以将&替换为&

代码语言:javascript
复制
@echo off
set grep=binaries\grep.exe
set fileindex=binaries\index.htm
set count=0

for /f "tokens=* delims=" %%a in (crc.dat) do ( 
    set /a count+=1
    %grep% --ignore-case -w "%%a" %fileindex%>>temp.dat
)
powershell -Command "(gc temp.dat) -replace '&', '&' | sc list.dat"
del /f temp.dat
pause
exit /b

有些改变了:

  • set /a count+=1 = set /a count=%count%+1
    • !fileHash!指向%%a,那么我们为什么需要它?%%a会工作的。

  • powershell命令将&替换为&
  • 将替换的内容输出到文件中。
  • 删除使用的临时文件。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46905407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档