编辑:是的,这必须批量完成。
我需要能够读取输入文件,并解析出仅包含特定字符串的行的某些部分,然后将其写入输出文件。例如:
输入 =
i_NumberOfPersonInTheDataBase=1
i_NumberOfPersonInTheDataBase < 50 AdjTotal=801
MATCH IdentificationResult id=Olivier Score=11419 (NOT_DEFINED-cfv )
02-11-11-07-00 TAG_CAPTURE Badge:CAPTURE - Candidate Found :Olivier
i_NumberOfPersonInTheDataBase=1
i_NumberOfPersonInTheDataBase < 50 AdjTotal=801
MATCH IdentificationResult id=Martin Score=1008 (NOT_DEFINED-cfv )
02-11-11-08-15 TAG_CAPTURE Badge:CAPTURE - Candidate Found :Martin
在包含字符串“IdentificationResult”的行中,我需要返回包含id和Score的字符串。
期望输出 =
id=Olivier Score=11419
id=Martin Score=1008
这是我目前的进展:
这里是我到目前为止的工作:
@setlocal enableextensions enabledelayedexpansion
:: Path of input and output files
set INPUTFILE=DemoFingerOtf-2.log
set OUTPUTFILE=logOutput.txt
:: Clear out the output file
@echo on > %OUTPUTFILE%
:: Read %INPUTFILE% and loop through each line
for /F "tokens=* delims=" %%A in (%INPUTFILE%) do (
SET my_line=%%A
SET my_line=!my_line:IdentificationResult=!
if not !my_line!==%%A (
call :parse_it
)
)
:parse_it
for /F "usebackq tokens=1,2,3,4 delims=~" %%1 in ('%my_line: =~%') do (
echo %%3 %%4>> %OUTPUTFILE%
)
我现在遇到的问题是,当我运行这个脚本时,会出现') was unexpected at this time错误。当我从输入中删除括号时,就能得到我预期的结果。我已经尝试过添加以下行来删除括号:
:: Read %INPUTFILE% and loop through each line
for /F "tokens=* delims=" %%A in (%INPUTFILE%) do (
SET my_line=%%A
SET my_line=!my_line:IdentificationResult=!
if not !my_line!==%%A (
SET new_line=%my_line:~0,-18%
call :parse_it
)
)
:parse_it
for /F "usebackq tokens=1,2,3,4 delims=~" %%1 in ('%new_line: =~%') do (
echo %%3 %%4>> %OUTPUTFILE%
)
我知道在我需要的那几行中,带括号的部分总是恰好18个字符,所以我从末尾删除它们。然而,当我这样做时,由于某种原因,我的输出结果如下:
错误的输出:
id=Olivier Score=11419
id=Olivier Score=11419
id=Olivier Score=11419
所以,我只想解析第一行中的数据,但是我却获取了三次(尽管我的输入中只有两行符合我的条件)。为什么我会多次获取这些数据而不是正确的数据?此外,是否有更好的方法解决我遇到的') was unexpected at this time错误?
id=Olivier Score=11419 ScoreAdjustment=801
这样的行,因此输出末尾会有一个额外的“ScoreAdjustment”,而我不想要它。是否有适用于这种情况的解决方案? - preppypoof