我想在日志文件中找到单词passed
或 failed
,并将匹配成功的行保存在输出文件中。我使用findstr
命令编写了一个小的批处理脚本。它似乎不起作用。
if exist
条件下使用变量时,这很奇怪。findstr
命令什么也不返回。我是批处理编程的初学者,所以也许我错过了一些批处理概念的理解。
@echo off
set Input = simulation_results.log
set Output = sim_catchedmsg.log
if exist %Output% del %Output%
for /f "tokens=*" %%a in (%Input%) do (
Set line=%%a
findstr /X "passed failed" %line% >> %Output%
pause)
输入日志文件:
<time="1500 ns" instance="testcase_00">passed
<time="342100480 ps" instance="testcase_01">passed
blabla informations about the tests....
<time="742894 ns" instance="testcase_02_01">passed
blabla informations about the tests....
blabla informations about the tests....
<time="744121040 ps" instance="testcase_02_02">failed
blabla informations about the tests....
<time="745034560 ps" instance="testcase_02_03">passed
blabla informations about the tests....
<time="745134560 ps" instance="testcase_02_04">passed
blabla informations about the tests....
blabla informations about the tests....
blabla informations about the tests....
blabla informations about the tests....
<time="745548080 ps" instance="testcase_03">failed
<time="747388640 ps" instance="testcase_04_01">passed
<time="750745100 ns" instance="testcase_04_02">passed
blabla informations about the tests....
答案 0 :(得分:2)
您的代码有几个问题。
1)不要在SET
命令中在等号的两侧放置空格。
2)您错误地使用了FINDSTR
命令。 line
变量不是文件。 FINDSTR
命令期望最后一个参数是文件名。
3)如果您确实想在FINDSTR
命令中执行FOR
,则会延迟扩展问题。
您需要做的就是这个。
@echo off
set "Input=simulation_results.log"
set "Output=sim_catchedmsg.log"
if exist "%Output%" del "%Output%"
findstr "passed failed" "%input%">>"%Output%"