2016-09-21 109 views
2

大家好,我有一个任务来分析这样一个有些日志文件:批次:计算时间从时间戳经过

12:03:11 Testing Import (test #240.) 
12:10:47 Testing Searches (test #241.) 
12:14:39 Testing Default notifications (test #242.) 
12:20:05 Testing Inventory list monitor (test #243.) 
12:34:31 Testing Reports from console (test #244.) 
12:40:05 Testing Users and user groups (test #245.) 
12:43:33  ERROR: 1024 characters in field Name, warning not shown! 
12:48:13 Testing RDE (test #246.) 

和我的工作是计算每个测试运行时间,并将其写入到另一个TXT文件。当然行这样一个

12:43:33 ERROR: 1024 characters in field Name, warning not shown!

不应予以考虑。所以这个字符串(test #可以作为某种标识符。最好的语言是批处理,但我真的有限的经验,所以任何帮助将是有用的。谢谢!

+0

是批次的要求?它并不是最适合这样的任务;甚至可以做,如果没有其他选项,但通常我会建议像python甚至像graylog等专用日志解析解决方案。 – Drako

+2

没有日期条目,所以持续时间计算可能不可靠... – aschipfl

+2

请注意https://stackoverflow.com不是一个免费的脚本/代码写作服务。如果您告诉我们您迄今为止所尝试的内容(包括您已经使用的脚本/代码)以及您卡住的位置,那么我们可以尝试帮助解决特定问题。您还应该阅读[我如何提出一个好问题?](https://stackoverflow.com/help/how-to-ask)。 – DavidPostill

回答

2
@ECHO OFF 
SETLOCAL 
SET "sourcedir=U:\sourcedir" 
SET "destdir=U:\destdir" 
SET "filename1=%sourcedir%\q39619700.txt" 
SET "outfile=%destdir%\outfile.txt" 
SET "starthh=" 
(
FOR /f "tokens=1-3*delims=: " %%a IN (
'findstr /L /c:"(test #" "%filename1%"') DO (
IF DEFINED starthh (
    CALL :report 1%%a 1%%b 1%%c 
) ELSE (
    SET /a starthh=1%%a&SET /a startmm=1%%b&SET /a startss=1%%c 
) 
SET "event=%%d" 
) 
)>"%outfile%" 

GOTO :EOF 

:report 
SET /a starthh=((%1-starthh)*3600)+((%2-startmm)*60)+%3-startss 
IF %starthh% lss 0 SET /a starthh+=3600*24 
:: If in seconds 
ECHO %starthh% seconds FOR %event% 
:: If in hh:mm:ss 
SET /a startss=100+(starthh %% 60) 
SET /a startmm=100+((starthh-startss+100) %% 3600)/60 
SET /a starthh=100+(starthh/3600) 
ECHO %starthh:~-2%:%startmm:~-2%:%startss:~-2% FOR %event% 
SET /a starthh=%1 
SET /a startmm=%2 
SET /a startss=%3 
GOTO :eof 

你需要改变的sourcedirdestdir设置以适合你的情况。

我用了一个包含您的数据,我的测试命名为q39619700.txt文件。

可生产定义为%OUTFILE%

对于日志中的每个条目的文件时,通过使用findstring以定位仅那些含有(test #线,记录开始时间的元素和事件文本过滤。

处理仅仅是中减去从旧新的时间要素,计算结果以及再现与所计算出的经过时间的旧的事件文本的问题;然后从最新行重新设置时间元素。请注意在时间元素字符串之前使用1来克服前导零 - 八分之一问题。

+1

太棒了,只是我需要的东西。一旦我拥有足够的声望,你就会得到你的+1。 – Wanderer