1

guys, i have a task to analyze some log files like this one:

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.)

and my job would be to calculate time elapsed on each test and write it to another txt file. Of course lines like this one

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

should not be taken into account. So this string (test # can be taken as some kind of identifier. Preferable language is batch but I have really limited experience with it so any help would be useful. Thank you!

6
  • is batch a requirement? it's not really best suited for such tasks; even can be done if no other option, but normally I would suggest something like python or even dedicated log-parsing solution like graylog, etc. Commented Sep 21, 2016 at 14:46
  • 2
    There is no date entry, so duration calculations might not be reliable... Commented Sep 21, 2016 at 15:07
  • 2
    Please note that stackoverflow.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?. Commented Sep 21, 2016 at 16:50
  • @Vairis: batch is not a requirement, but i need something that will work natively on Windows. Python is great, and I more familiar with it, but I would have to install it on that specific system. Commented Sep 22, 2016 at 7:39
  • @aschipfl: I'm aware of that, thank You. I will think of something. . Commented Sep 22, 2016 at 7:48

1 Answer 1

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

You would need to change the settings of sourcedir and destdir to suit your circumstances.

I used a file named q39619700.txt containing your data for my testing.

Produces the file defined as %outfile%

For each entry in the log, filtered by using findstring to locate only those lines containing (test #, record the start time elements and the event text.

Processing is simply a matter of subtracting the new time elements from the old, calculating the result and reproducing the old event text with the calculated elapsed time; then re-setting the time elements from the latest line. Note the use of 1 preceding the time-element strings to overcome the leading-zero-is-octal problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic, just the thing I need. You will get your +1 once I have sufficient reputation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.