我需要批處理 commnad 來 findstr 并將文本檢索到文本檔案的末尾。
文本檔案包括
# 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 Response time is less than 100ms”
uj5u.com熱心網友回復:
我相信這就是您所需要的:
不清楚資料中的空行實際上是空的,還是包含一個、兩個或更多空格。該代碼適用于任何 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 :EOF
repro是確定是否重現該行的標志。
每行都以 開頭編號,因此 之前的部分[linenumber]分配給,實際行內容分配給。find]%%b%%c
mtline然后用作%%c不支持的子字串元變數(如 );它收到一份副本,%%c如果它不為空,則洗掉所有空格。
ifmtline然后為空,批處理將其解釋為undefined.
如果mtline被定義,則代碼判斷是否repro被定義,如果被定義則echo回傳es %%c,否則詢問findstr該行是否包含目標字串;如果是,則設定repro一個值(使它defined和echo線。
如果mtline未定義,那么我們已經到達該部分的末尾,因此echo空行清晰repro,因此不會復制更多行。
^<isescaped redirector告訴cmd重定向程式是命令的一部分,find而不是for.
整個for命令都包含在括號中,這樣echo它執行的 es 就可以重定向到一個檔案中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/535944.html
標籤:批处理文件命令
上一篇:無法使用electron-builder構建應用程式。錯誤:gyp失敗,退出代碼:1
下一篇:閉包
