我有一个日志文件,用于监视包括请求和确认在内的大型系统。我的目标是能够:批处理脚本来抓取linestr而不使用文件路径
1.遍历脚本,并获得其中要求&它们的确认发生
2.重要性的整个线拉为字符串行,并将其保存为字符串修改输出到其他地方的变量。
这是我到目前为止有:
@ECHO off
setlocal
setlocal enabledelayedexpansion
setlocal enableextensions
:: Lets get today's date, formatted the way the ABCD File is named
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do set targetDate=%%f-%%d-%%e
:: Now we set the targetFile name
SET ABCDLogsFile=C:\Users\me\Documents\monitoring_file_for_jim\ABCDFIX*%targetDate%.log
::****Scrapped original approach*****
set "ackFoundCount=0"
set "reqFoundCount=0"
::Get lines with acks
for /f delims^=^ eol^= %%a in ('findstr /c:"\<ACK\>" "%ABCDLogsFile%"') do (
set /a "ackFoundCount+=1"
setlocal enabledelayedexpansion
for %%N in (!ackFoundCount!) do (
endlocal
set "ackFound%%N=%%a"
)
)
::Get lines with requests
for /f delims^=^ eol^= %%b in ('findstr /c:"ReqSingle" "%ABCDLogsFile%"') do (
set /a "reqFoundCount+=1"
setlocal enabledelayedexpansion
for %%N in (!reqFoundCount!) do (
endlocal
set "reqFound%%N=%%b"
)
)
setlocal enabledelayedexpansion
for /l %%N in (1,1,2 %reqFoundCount%) do echo REQ %%N FOUND= !reqFound%%N!
pause
for /l %%N in (1,1,2 %ackFoundCount%) do echo ACK %%N FOUND= !ackfound%%N!
endlocal
EDIT 2 dbenham
我试图完成,这是完全不必要的前迂回的方式。 多亏了问题和答案在这里:
'findstr' with multiple search results (BATCH)
我有我的剧本和类似的工作。然而,我很好奇它是否有可能在开始时不使用文件路径而获得findstr输出。我只需要将日志中的时间戳记串出来,这将始终是每行的前12个字符(没有文件路径)。我的输出目前以路径为前缀,虽然我可以获得日志最终将在生产中的路径,但以另一种方式尝试并执行操作会更安全。在这个脚本最终运行的时候,每个只有1个或2个请求和答案,这就是为什么我存储所有被发现的。这不是必要的,但我认为如果有两个是两个,那么看到两个就会让人放心。下面是输出看起来像ACK和请求数一样东西:
C:\Users\me\Documents\monitoring_file_for_jim\ABCDFIX 2015-04-01.log:2015-03-26 07:00:11,028 INFO etc...
我在想,如果我能剥离掉的文件路径的开始,然后所有我需要做的就是只是时间戳的事件将是
for /l %%N in (1,1,1 %reqFoundCount%) do echo Req %%N occurred at: !reqFound%%N:~0,12! >> MorningAckChecks.txt
for /l %%N in (1,1,1 %ackFoundCount%) do echo ACK %%N occurred at: !ackfound%%N:~0,12! >> MorningAckChecks.txt
>“我试图将这些变量作为跳过的参数使用for/f,并抓住这些行,但我似乎无法正确地做到这一点。” - 请张贴此代码。 – 2015-03-31 19:07:13
对不起。 – driftandwander 2015-03-31 19:29:43
不容易使用powershell!只是'Select-String -Pattern“stack”-List -SimpleMatch -Path F:\ Andorid \ Andorid.vmx |选择Line,LineNumber',你可以得到线条与你的图案相匹配,并且非常简单! – powershell 2015-03-31 22:06:21