I need batch commnad to findstr and retrieve text to the end of text file.
text file include
# 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"
I need to find "AssertionError" and retrieve text to the end of text file.
I expect to show since "AssertionError" untill the end of file.
I try this.
@echo off
setlocal
for /F "tokens=* delims=" %%a in ('findstr /I "AssertionError" test1.log') do set "uniuser=%%a"
echo User is: %uniuser%
endlocal
and it 's show only " 2. AssertionError Response time is less than 100ms"
CodePudding user response:
I believe that this is what you need:
It's not clear whether the empty lines in your data are actually empty, or contain one, two, or more spaces. The code caters for any pf these situations.
@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 :EOF
repro
is a flag to determine whether to reproduce the line or not.
Each line is numbered with a leading [linenumber]
by the find
so the part before the ]
is assigned to %%b
and the actual line contents to %%c
.
mtline
is then used as substringing metavariables (like %%c
) is not supported; it receives a copy of %%c
and if this is not empty, removes any spaces.
if mtline
is then empty, batch interprets it as undefined
.
if mtline
is defined, the code then determines whether repro
is defined and echo
es %%c
if it is defined, otherwise it asks findstr
whether the line contains the target string; if it does, then set repro
to a value (making it defined
and echo
the line.
if mtline
is not defined, then we have reached the end of the section, so echo
an empty line and clear repro
so no further lines are reproduced.
The ^<
is an escaped redirector
which tells cmd
that the redirector is part of the find
command, not the for
.
The entire for
command is enclosed in parentheses so that the echo
es it executes can be redirected to a file.