在分批脚本中将Parsed值设置为变量

时间:2015-12-17 12:38:22

标签: parsing batch-file scripting

所以我很难用这个,我试图解析一个特定值的txt文件并将其添加到变量,但只是没有让它正常工作我使用以下代码

for /F "tokens=5 delims= " %%a in ('findstr /I "Short part of line 00:00" test.txt') do set "var1=%%a"

问题在于,还有其他类似的文本行略有不同,我在整体考虑时会包含一个独特的短语,但我不认为该命令会考虑到这一点?该文件在大小方面总是相同的,无论如何重写这个以找到特定行上的标记并转而分配给变量?

编辑:

input.txt

Process 1                   

Primary process Started            2:50:00
Primary Last part process          3:40:52
Primary part 3 Balanced            5:01:55
Primary CMD Backup Completed       5:04:13
Primary Process Balance Completed  5:04:13


Process 2       

Secondary process Started            2:50:00
Secondary Last part process          3:42:08
Secondary part 3 Balanced            5:06:38
Secondary CMD Backup Completed       5:08:56
Secondary Process Balance Completed  5:08:56

所以,如果我要使用命令:

for /F "tokens=5 delims= " %%a in ('findstr /I "Primary process Started" test.txt') do set "var1=%%a"

我应该回读2:50:00而不是我得到5:08:56这是奇怪的一个,其余的只是翻转所以“主要部分3平衡”将给我回到“第二部分3”的时间平衡的“

2 个答案:

答案 0 :(得分:1)

根据你的意见,这应该有效:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%f in (input.txt) do (
    set line=%%f
    if "!line:~0,23!"=="Primary process Started" (
        for /f "tokens=4" %%t in ("%%f") do set var1=%%t
        goto BREAK
    )
)
:BREAK
echo %var1%

答案 1 :(得分:1)

稍微简化@MichaelS代码。

@echo off
for /f "tokens=1-4 delims= " %%G in (input.txt) do (
    if "%%G %%H %%I"=="Primary process Started" (
        set var1=%%J
        GOTO BREAK
    )
)
:BREAK
echo %var1%