如何使用FINDSTR将包含PASSED或FAILED的日志文件中的所有行写入新文件?

时间:2018-08-01 14:16:16

标签: batch-file variables findstr

我想在日志文件中找到单词passed failed,并将匹配成功的行保存在输出文件中。我使用findstr命令编写了一个小的批处理脚本。它似乎不起作用。

  1. 当我在if exist条件下使用变量时,这很奇怪。
  2. 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....

1 个答案:

答案 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%"