Computing.Net > Forums > Programming > Batch file to parse log

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to parse log

Reply to Message Icon

Name: Da Puff
Date: April 20, 2008 at 07:31:14 Pacific
OS: XP Pro 32bit
CPU/Ram: Intel Core Duo / 3 GB
Product: Dell M90
Comment:

Hello,

I am trying to create a batch file to parse a log file, find some timestamps in a specific format, change the format of the timestamp, then display the timestamp in the command window. My current code is working exactly as I'd like it to, but I am running into a problem with a specific line in my log file. It seems my code doesn't like the line with the parentheses. How can I adjust my code to make it read past this line?

----Start batch file code-------
@echo off
:parse_log
setLocal EnableDelayedExpansion

for /f "eol=> tokens=* delims= " %%a in (%1) do (
set for_line=%%a
call :parse
)

:parse
echo %for_line% | find "TIMESTAMP" >NUL
if not errorlevel 1 (
set str=%for_line%
call :split_string
)
goto :EOF

:split_string
:: Find the time part of the timestamp
for /f "tokens=2,3 delims=^-" %%a in ('echo !str!') do (
set timestamp=%%a
set timestamp_comment=%%b
set finaltimestamp=!timestamp:~1,11!
set finaltimestampcomment=!timestamp_comment:~2,-2!
)

echo !finaltimestamp! - !finaltimestampcomment!
goto :EOF
----End batch file code-----

----Start example log file text-----
lots of text I don't care about...
lots of text I don't care about...
lots of text I don't care about...

> !batch_currenttime.bat "Domain reordered"
TIMESTAMP - 14:10:04.02 - "Domain reordered"

> /define/models/radiation/s2s-parameters/read-vf-file example.cas.gz.s2s.gz
Reading "| gunzip -c "D:/Projects/2008/Fluent_Radiaton/Raytracing2/testing/smalltest/example.cas.gz.s2s.gz""...
cells........
Reading radiating clusters (7463) and view factor values from D:/Projects/2008/Fluent_Radiaton/Raytracing2/testing/smalltest/example.cas.gz.s2s.gz...
,
Initialized VF Storage
Done.

> !batch_currenttime.bat "Starting 1st order iterations"
TIMESTAMP - 14:10:39.56 - "Starting 1st order iterations"
----End example log file text-----



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: April 20, 2008 at 10:54:26 Pacific
Reply:

This doesn't so much fix the problem as avoid it. Maybe IVO can show an elegant solution.

Note that you omitted a goto :eof after your first FOR loop, causing it to stutter.

::==

    @echo off > clean
:parse_log
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (%1) do (
::echo %%a
set str=%%a
set str=!str:^(=[!
set str=!str:^)=]!
echo !str!>> clean
)
for /f "eol=> tokens=* delims= " %%a in (clean) do (
set for_line=%%a
call :parse
)
goto :eof
:parse
echo %for_line% | find "TIMESTAMP" >NUL
if not errorlevel 1 (
set str=%for_line%
call :split_string
)
goto :EOF

:split_string
:: Find the time part of the timestamp
for /f "tokens=2,3 delims=^-" %%a in ('echo !str!') do (
set timestamp=%%a
set timestamp_comment=%%b
set finaltimestamp=!timestamp:~1,11!
set finaltimestampcomment=!timestamp_comment:~2,-2!
)

echo !finaltimestamp! - !finaltimestampcomment!
goto :EOF


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: Mechanix2Go
Date: April 20, 2008 at 10:59:07 Pacific
Reply:

One nit-picky note: you trimmed the end quote AND the last ;etter of the string, yielding:

14:10:04.02 - Domain reordere
14:10:39.56 - Starting 1st order iteration


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 3
Name: Mechanix2Go
Date: April 20, 2008 at 12:22:31 Pacific
Reply:

More imply:

@echo off > newfile
setLocal EnableDelayedExpansion

find "TIMESTAMP" < %1 > %temp%\##

for /f "tokens=2* delims=^- " %%a in (%temp%\##) do (
set str=%%a - %%b
set str=!str:"=!
echo !str! >> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: Mechanix2Go
Date: April 20, 2008 at 14:34:54 Pacific
Reply:

version 3:

@echo off > newfile
setLocal EnableDelayedExpansion

for /f "tokens=2* delims=^- " %%a in ('find "TIMESTAMP" ^< %1') do (
set str=%%a - %%b
set str=!str:"=!
echo !str! >> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 5
Name: Da Puff
Date: April 20, 2008 at 16:04:40 Pacific
Reply:

Thanks M2 for your help! The code works great. I'm still trying to figure out exactly what you did, but thank you! Also, the trimming of the last quote works fine on my system (with the last letter intact)... maybe it was due to my cut/paste of the log file into the first post.

Thanks again for your help!


0

Related Posts

See More



Response Number 6
Name: klint
Date: April 21, 2008 at 10:19:01 Pacific
Reply:

M2's got the best solution, but just for the record, the reason your original script was broken was this line inside a for loop:

set str=%for_line%

When for_line contained the line with the brackets, the command interpreter took the closing bracket to mean the end of the for loop. Remember, "%" expansion is done before the line is parsed. You should use delayed expansion inside for loops (i.e. use "!" instead of "%".)


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to parse log

Batch File to Parse and Write back www.computing.net/answers/programming/batch-file-to-parse-and-write-back/18101.html

batch file to parse text file www.computing.net/answers/programming/batch-file-to-parse-text-file/18030.html

Batch file to concatenate files www.computing.net/answers/programming/batch-file-to-concatenate-files/12391.html