You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求定位AssertionError并提取至文件末尾的Batch findstr命令

问题

需要编写Batch命令,定位日志文件test1.log中的第一个AssertionError,并提取从该内容开始到文件末尾的全部文本。

日志文件test1.log内容如下:

#  failure         detail                           
                                                      
 1.  AssertionError  Response time is less than 100ms 
                     expected false to be truthy       
                     at assertion:1 in test-script     
                     inside "epos-agentDetailsInfo"    
   
 2.  AssertionError  Response time is less than 100ms 
                     expected false to be truthy       
                     at assertion:1 in test-script     
                     inside "epos-getccounts"  

当前尝试的脚本仅能输出最后一行包含AssertionError的内容,无法实现需求:

@echo off
setlocal
for /F "tokens=* delims=" %%a in ('findstr /I "AssertionError" test1.log') do set "uniuser=%%a"  
echo User is: %uniuser%   

endlocal
解决方案

通过设置标记变量遍历日志行,首次匹配到目标内容后持续输出后续所有行,脚本如下:

@echo off
setlocal enabledelayedexpansion

set "printFlag="
for /F "delims=" %%a in (test1.log) do (
    if defined printFlag (
        echo %%a
    ) else (
        echo %%a | findstr /I "AssertionError" >nul
        if not errorlevel 1 (
            set "printFlag=1"
            echo %%a
        )
    )
)

endlocal

关键说明

  • enabledelayedexpansion:确保循环内变量printFlag的修改能实时生效。
  • 标记逻辑:初始printFlag为空,不输出行;首次匹配到AssertionError后,设置标记并输出当前行,后续所有行直接输出。
  • 输出保存:若需将结果写入文件,把echo %%a替换为echo %%a >> output.log即可。

内容的提问来源于stack exchange,提问作者วิชา ประเทศ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 19:25:28