我需要创建一个批处理文件[不幸的是它必须是一个批处理文件:(]从日志中提取信息,将该数据放入一个新文件,然后从新文件中将错误文本拉入另一个文件所以我最终应该得到3个文件。原始日志,拉线程和最终日志。
线程编号和错误文本通常是随机的,需要用户输入,以便提取正确的线程以及正确的错误。
非常感谢任何和所有帮助。
我的日志文件可能如下所示
在这个例子中,我正在寻找线程1234.一旦它在自己的文件中,我想要提取错误文本。连同从头到尾的线。所以我的最终文件看起来像下面的内容。
错误文本上方和下方的行数可能会发生很大变化。我的初步批处理文件如下。
@echo off
cls
:start
cls
:: Context Menu
echo ____________________________________________________________
echo ^| Please choose an option below. ^|
echo ^|Search logs for specific error? [B] ^|
echo ^|Exit [X] ^|
echo ^|___________________________________________________________^|
:: Directory
set /p choice=" "
if '%Choice%' == 'B' goto :Searchlogs
if '%Choice%' == 'b' goto :Searchlogs
if '%Choice%' == 'X' goto :exit
if '%Choice%' == 'x' goto :exit
if '%Choice%' == ' ' echo "%Choice%" is not a valid option. Please try again.
if not %choice% == set choice =%choice:~0,1%
echo "%Choice%" is not a valid option. Please try again.
TIMEOUT /T -1
goto :start
cls
goto :start
TIMEOUT /T -1
:exit
cls
exit
:Searchlogs
echo Please enter the Thread number in format "1234"
copy \\Servername\log\logfile.log c:\users\%username%\Desktop\logfile.txt
copy \\Servername\log\logfile-1.log c:\users\%username%\logs\logfile-1.txt
set /p threadnumber=" "
find " %threadnumber%:" C:\Users\%username%\Desktop\logfile*.txt >C:\Users\%username%\Desktop\%threadnumber%.txt
echo Please paste in error text.
set /p Errortext=" "
find "%Errortext%" C:\Users\%username%\Desktop\%threadnumber%.txt" >C:\Users\%username%\Desktop\Error.txt
TIMEOUT /T -1
goto :start
答案 0 :(得分:0)
type file.log | Findstr /c:"Thread 1234" > FileWithThreadInIt.txt
type FileWithThreadInIt.txt | Findstr /c:"Error" >FileWithThreadErrors.txt
type FileWithThreadInIt.txt | Findstr /v /c:"Error" >FileWithThreadNoErrors.txt
type file.log | Findstr /v /c:"Thread 1234" > FileWithOtherThreads.txt
使用变量
Set /p Thread=Enter thread number
type file.log | Findstr /c:"Thread %Thread%" > FileWithThreadInIt.txt
答案 1 :(得分:0)
@ECHO OFF
SETLOCAL
SET /p "findme=Identifier to find (eg thread 1234)"
SET /p "errortext=Error text to find (eg ERROR ERROR ERROR)"
FINDSTR /c:"%findme%" <q23318892.txt >threadfile.txt
SET "block="
SET "errorblock="
FOR /f "delims=" %%a IN (threadfile.txt) DO (
ECHO(%%a|FIND "%findme%: Start" >NUL
IF NOT ERRORLEVEL 1 SET block=Y
IF DEFINED block >>threaderror.txt ECHO(%%a
ECHO(%%a|FIND "%findme%: %errortext%" >NUL
IF NOT ERRORLEVEL 1 SET errorblock=Y
ECHO(%%a|FIND "%findme%: End" >NUL
IF NOT ERRORLEVEL 1 (
IF DEFINED errorblock GOTO done
IF DEFINED block DEL threaderror.txt
SET "block="
SET "errorblock="
)
)
:done
IF EXIST threaderror.txt (TYPE threaderror.txt) ELSE (ECHO threaderror.txt NOT created)
GOTO :EOF
此解决方案假定需要第一个“错误”块。您需要设置要查找的文本和文件名。我使用了一个名为q23318892.txt
的文件,其中包含我的测试数据。
已修复以满足进一步的要求:允许用户输入
答案 2 :(得分:0)
@echo off
setlocal EnableDelayedExpansion
:Searchlogs
copy \\Servername\log\logfile.log c:\users\%username%\Desktop\logfile.txt
copy \\Servername\log\logfile-1.log c:\users\%username%\logs\logfile-1.txt
echo Please enter the Thread number in format "1234"
set /p threadnumber=
find " %threadnumber%:" C:\Users\%username%\Desktop\logfile*.txt > C:\Users\%username%\Desktop\%threadnumber%.txt
echo Please paste in error text.
set /p Errortext=
set i=0
set "errorFound="
(for /F "tokens=1-4*" %%a in (C:\Users\%username%\Desktop\%threadnumber%.txt) do (
set /A i+=1
set "line[!i!]=%%a %%b %%c %%d %%e"
if "%%e" equ "End" (
if defined errorFound (
for /L %%i in (1,1,!i!) do echo !line[%%i]!
echo/
)
set i=0
set "errorFound="
) else if "%%e" equ "%Errortext%" (
set errorFound=true
)
)) > C:\Users\%username%\Desktop\Error.txt
示例:
Please enter the Thread number in format "1234"
1234
Please paste in error text.
ERROR ERROR ERROR
01/01/01 12:00:00: thread 1234: Start
01/01/01 12:00:00: thread 1234: *: Other userful information
01/01/01 12:00:00: thread 1234: *: Other userful information
01/01/01 12:00:00: thread 1234: ERROR ERROR ERROR
01/01/01 12:00:00: thread 1234: *: Other userful information
01/01/01 12:00:00: thread 1234: *: Other userful information
01/01/01 12:00:00: thread 1234: End