File parsing in Batch

June 16, 2011 at 18:07:20
Specs: Windows XP
I am trying to parse a log file and calculate statistics. My log file looks like this -

......(data not relevant to this requirement)
DIRECTOR> TM_6252 Source Load Summary.
DIRECTOR> CMN_1740 Table: [SQ_Table1] (Instance Name: [SQ_Table1])
Output Rows [3], Affected Rows [3], Applied Rows [3], Rejected Rows [0]
DIRECTOR> CMN_1740 Table: [SQ_Table2] (Instance Name: [SQ_Table2])
Output Rows [6], Affected Rows [6], Applied Rows [6], Rejected Rows [0]
DIRECTOR> TM_6253 Target Load Summary.
DIRECTOR> CMN_1740 Table: [Table1] (Instance Name: [ins_Table1])
Output Rows [2], Affected Rows [2], Applied Rows [2], Rejected Rows [0]
DIRECTOR> CMN_1740 Table: [Table2] (Instance Name: [ins_Table2])
Output Rows [5], Affected Rows [5], Applied Rows [5], Rejected Rows [0]
DIRECTOR> CMN_1740 Table: [Table3] (Instance Name: [del_Table3])
Output Rows [3], Affected Rows [3], Applied Rows [3], Rejected Rows [0]
DIRECTOR> CMN_1740 Table: [Table4] (Instance Name: [upd_Table4])
Output Rows [3], Affected Rows [3], Applied Rows [3], Rejected Rows [1]
DIRECTOR> TM_6023

I need the following counts from here -

Source Applied = 9 (Sum of Applied Rows from TM_6252 section)
Source Rejected = 0 (Sum of Rejected Rows from TM_6252 section)
Target Inserted = 7 (Sum of Applied Rows from TM_6252 section where Instance Name begins with ins)
Target Updated = 3 (Sum of Applied Rows from TM_6252 section where Instance Name begins with upd)
Target Deleted = 3 (Sum of Applied Rows from TM_6252 section where Instance Name begins with del)
Target Rejected = 1 (Sum of Rejected Rows from TM_6252 section)

I was able to do the Source Counts and the Target Rejected count. But I haven't managed the others.

For the Target counts, this is what I tried -

for /F "tokens=1,* delims=[]," %%i in ('find /n "TM_6253 Target Load Summary" %file_name%') do SET LineNoTgt=%%i

for /F "skip=%LineNoTgt% tokens=1-13* delims=[],:()> " %%a in (%file_name%) do (

if %%b==TM_6023 goto End

if %%b==CMN_1740 (
@echo %%g > %file_name%.tmp
for /F "tokens=1* delims=_, " %%o in (%file_name%.tmp) do @echo %%o > %file_name%.tmp2
) else (
for /F "tokens=*" %%r in (%file_name%.tmp2) do (
if %%r==ins SET /a TotalInsertCount=TotalInsertCount+%%j
if %%r==upd SET /a TotalUpdateCount=TotalInsertCount+%%j
if %%r==del SET /a TotalDeleteCount=TotalInsertCount+%%j
)
)

if %%k==Rejected SET /a TotalRejectedCount=TotalRejectedCount+%%m

)

:End
del %file_name%.tmp
del %file_name%.tmp2

@echo Insert count is %TotalInsertCount%
@echo Update count is %TotalUpdateCount%
@echo Delete count is %TotalDeleteCount%
@echo Reject count is %TotalRejectedCount%

What I am doing wrong here?

Thanks,
Ravi


See More: File parsing in Batch

Report •


#1
June 16, 2011 at 20:27:02
I've always had to SET skip=skip=%LineNoTgt%, and use %skip% in the FOR loop.

for /F "skip=%LineNoTgt% tokens=1-13* delims=[],:()> "

would turn into:

set skip=skip=%LineNoTgt%
for /F "%skip% tokens=1-13* delims=[],:()> "

I didn't really look at or test any of the other logic; just the "skip" portion jumped out at me at first glance.

When your only tool is a hammer, every problem looks like a nail.


Report •

#2
June 17, 2011 at 10:01:17
skip seems to be working fine. It is the SET for the Target counts that is not working.

Report •

#3
June 17, 2011 at 14:51:13
Not sure what the difference is, but when I use ! instead of % inside the for loop, I was able to calculate the counts.

:Target
for /F "skip=%LineNoTgt% tokens=1-13* delims=[],:()> " %%a in (%file_name%) do (

if %%b==TM_6023 goto End


if %%b==CMN_1740 (
SET InstanceName=%%g
for /F "tokens=1,* delims=_" %%n in ('@echo !InstanceName!') do SET InstanceType=%%n
) else (
if !InstanceType!==ins SET /a TotalInsertCount=TotalInsertCount+%%j
if !InstanceType!==upd SET /a TotalUpdateCount=TotalUpdateCount+%%j
if !InstanceType!==del SET /a TotalDeleteCount=TotalDeleteCount+%%j
)


if %%k==Rejected SET /a TotalRejectedCount=TotalRejectedCount+%%m

)


Report •

Related Solutions

#4
June 17, 2011 at 15:03:11
Are you sure your tokens are lining up as expected? By sight (but not tested in batch) I come up with:

a         b        c       d        e        f      g
DIRECTOR> CMN_1740 Table: [Table1] (Instance Name: [ins_Table1])
a      b     c   d        e     f   g       h     i   j        k     l 
Output Rows [2], Affected Rows [2], Applied Rows [2], Rejected Rows [0]

So here, I think you want to be adding %%i, not %%j:

            if %%r==ins SET /a TotalInsertCount=TotalInsertCount+%%j
            if %%r==upd SET /a TotalUpdateCount=TotalInsertCount+%%j
            if %%r==del SET /a TotalDeleteCount=TotalInsertCount+%%j

Also, it looks like you've carried the TotalInsertCount into the Update & Delete counts.
Try this instead:

            if %%r==ins SET /a TotalInsertCount+=%%i
            if %%r==upd SET /a TotalUpdateCount+=%%i
            if %%r==del SET /a TotalDeleteCount+=%%i

(+= is a quick way to add to the same variable)

When your only tool is a hammer, every problem looks like a nail.


Report •

#5
June 17, 2011 at 15:32:22
There is a Tab before Output, so the tokens are working correctly.
And thanks for tip on +=.

Any idea why !var! works in the for loop but not %var%?


Report •

#6
June 17, 2011 at 20:53:08
That's the syntax for DelayedExpansion. I imagine at the top of your script, you have:

SETLOCAL ENABLEDELAYEDEXPANSION

That allows variables to be expanded to their values while executing within the FOR loop, and not just at the time they are read in by the interpreter.

When your only tool is a hammer, every problem looks like a nail.


Report •


Ask Question