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

How to get all lines in a log file containing either PASSED or FAILED written into a new file using FINDSTR?

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

  1. 当我在 if exist 条件下使用变量时它不起作用,这很奇怪。
  2. findstr命令returns没有。

我是批处理编程的初学者,所以我可能对批处理的一些概念有误解。

@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) 不要在 SET 命令中等号两边放置空格。

2) 您错误地使用了 FINDSTR 命令。 line 变量不是文件。 FINDSTR 命令要求最后一个参数是文件名。

3) 如果你真的想在 FOR 命令中执行 FINDSTR,你有延迟扩展问题。

你只需要这样做。

@echo off
set "Input=simulation_results.log"
set "Output=sim_catchedmsg.log"
if exist "%Output%" del "%Output%"
findstr "passed failed" "%input%">>"%Output%"