我需要批处理逗号来查找和检索文本到文本文件的末尾。
文本文件包括
# 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“并将文本检索到文本文件的末尾。
我希望从"AssertionError“到文件结束都能显示出来。
我试试这个。
@echo off
setlocal
for /F "tokens=* delims=" %%a in ('findstr /I "AssertionError" test1.log') do set "uniuser=%%a"
echo User is: %uniuser%
endlocal它只显示“2. AssertionError响应时间小于100 is”。
发布于 2022-11-16 05:37:05
我相信这就是你所需要的:
还不清楚数据中的空行实际上是空的,还是包含一个、两个或多个空格。该代码适用于任何pf这种情况。
@ECHO OFF
SETLOCAL
rem The following settings for the source directory and filenames are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74454843.txt"
SET "outfile=%sourcedir%\q74454843.rpt"
SET "repro="
(
FOR /f "tokens=1*delims=]" %%b IN ('find /n /v "" ^<"%filename1%"') DO (
SET "mtline=%%c"
IF DEFINED mtline CALL SET "mtline=%%mtline: =%%"
IF DEFINED mtline (
IF DEFINED repro (
ECHO %%c
) ELSE (
ECHO %%c|FINDSTR /L /C:"AssertionError" >NUL
IF NOT ERRORLEVEL 1 (SET "repro=Y"&ECHO %%c)
)
) ELSE (
IF DEFINED repro ECHO.
SET "repro="
)
)
)>"%outfile%"
GOTO :EOFrepro是一个标志,用于决定是否再现该行。
每一行都由find用前导find编号,因此]之前的部分分配给%%b,实际的行内容分配给%%c。
然后,mtline被用作不支持子字符串元数据(如%%c);它接收到%%c的副本,如果该副本不是空的,则删除任何空格。
如果mtline为空,则批处理将其解释为undefined。
如果定义了mtline,那么代码将确定是否定义了repro,如果定义了%%c,则会询问findstr该行是否包含目标字符串;如果定义了,则将repro设置为值(使其为defined和echo行)。
如果没有定义mtline,那么我们已经到了部分的末尾,所以echo是一个空行,并且清除了repro,这样就没有更多的行被复制了。
^<是一个escaped redirector,它告诉cmd重定向器是find命令的一部分,而不是for。
整个for命令都包含在括号中,这样它执行的echoes就可以重定向到文件中。
https://stackoverflow.com/questions/74454843
复制相似问题