3

Edit: yes, this has to be done in batch.

I need to be able to read an input file, and parse out certain sections only of lines that contain a specific string, then write that to an output file. For example:

input =

i_NumberOfPersonInTheDataBase=1
i_NumberOfPersonInTheDataBase < 50 AdjTotal=801
MATCH IdentificationResult id=Olivier Score=11419 (NOT_DEFINED-cfv )
02-11-11-07-00 TAG_CAPTURE Badge:CAPTURE - Candidate Found :Olivier
i_NumberOfPersonInTheDataBase=1
i_NumberOfPersonInTheDataBase < 50 AdjTotal=801
MATCH IdentificationResult id=Martin Score=1008 (NOT_DEFINED-cfv )
02-11-11-08-15 TAG_CAPTURE Badge:CAPTURE - Candidate Found :Martin

in lines that contain the string "IdentificationResult", I need to return the strings that contain the id and Score.

expected output =

id=Olivier Score=11419
id=Martin Score=1008

This is what I have so far:

@setlocal enableextensions enabledelayedexpansion

:: Path of input and output files
set INPUTFILE=DemoFingerOtf-2.log
set OUTPUTFILE=logOutput.txt

:: Clear out the output file
@echo on > %OUTPUTFILE%

:: Read %INPUTFILE% and loop through each line
for /F "tokens=* delims=" %%A in (%INPUTFILE%) do (
    SET my_line=%%A
    SET my_line=!my_line:IdentificationResult=!
    if not !my_line!==%%A (
     call :parse_it
  )   
)

:parse_it
for /F "usebackq tokens=1,2,3,4 delims=~" %%1 in ('%my_line: =~%') do (
    echo %%3 %%4>> %OUTPUTFILE%
)

The problem I have right now is that when I run this script, I get a ') was unexpected at this time error. When I remove the parentheses from the input, I get my expected results. I've tried including a line like the following to remove the parentheses:

:: Read %INPUTFILE% and loop through each line
for /F "tokens=* delims=" %%A in (%INPUTFILE%) do (
    SET my_line=%%A
    SET my_line=!my_line:IdentificationResult=!
    if not !my_line!==%%A (
     SET new_line=%my_line:~0,-18%
     call :parse_it
  )   
)

:parse_it
for /F "usebackq tokens=1,2,3,4 delims=~" %%1 in ('%new_line: =~%') do (
    echo %%3 %%4>> %OUTPUTFILE%
)

I know that in the lines I want, the section with parentheses will always be exactly 18 characters, so I trim them from the end. However, when I do that, for some reason I get the following as my output:

wrong output:

id=Olivier Score=11419
id=Olivier Score=11419
id=Olivier Score=11419

So, I'm getting only the data from the first line that I want to parse, and I'm getting it three times (even though there are only two lines in my input that meet my criteria). Why am I getting this data multiple times instead of the correct data? Additionally, is there a better way around the ') was unexpected at this time error that I was getting?

preppypoof
  • 35
  • 1
  • 1
  • 5

4 Answers4

1

Edit modificated without trailing space. Without "ScoreAdjustment" and work with "John Smith" :)

echo off

:: Path of input and output files
set INPUTFILE=DemoFingerOtf-2.log
set OUTPUTFILE=logOutput.txt


setlocal enabledelayedexpansion
for /f "tokens=2,3 delims=^=^(" %%a in ('type "%INPUTFILE%" ^| find /i "IdentificationResult"') do (
                                                                   set $line=Id=%%a=%%b
                                                                   set $line=!$line:ScoreAdjustment=!
                                                                   set $line=!$line:~0,-1!
                                                                   echo !$line!>>%OUTPUTFILE%)

Endlocal
SachaDee
  • 8,843
  • 2
  • 18
  • 29
  • this is a lot simpler than what I was doing, but I don't fully understand what is going on here. specifically, what controls what values are stored in %%a and %%b? – preppypoof Dec 10 '13 at 21:26
  • just use `"tokens=2,3 delims==("`, no need for carets. And your output has a trailing space in every line. – Endoro Dec 10 '13 at 21:39
  • %%a (tokens 2) take the value between the "=" (delims ^=) delimiter [Olivier Score]and the %%b (tokens 3) take the value before the ")" (delims ^)) [11419] That's why i put the "Id=" before %%a and = after, to get the correct Output :) – SachaDee Dec 10 '13 at 21:40
  • yea, before the _opening_ parenthesis including one space! – Endoro Dec 10 '13 at 21:43
  • not for me :) but you can't call this `correct output`. Space is one space to much :) – Endoro Dec 10 '13 at 21:49
  • Thanks for the explanation. This works for most of my input, but some input lines have lines like `id=Olivier Score=11419 ScoreAdjustment=801`, so the output has an extra "ScoreAdjustment" at the end, which I don't want. Is there a solution that works for that too? – preppypoof Dec 10 '13 at 21:50
1
@ECHO OFF &SETLOCAL
for /f "delims=" %%a in ('^<file find "IdentificationResult"') do call:DOit "%%~a"
goto:Eof

:doit
setlocal
set "string=%~1"
set "STring=%string:*IdentificationResult=%"
for /f "Tokens=1,2" %%b in ("%string%") do echo(%%b %%c
exit /b
Endoro
  • 34,892
  • 8
  • 45
  • 61
  • well this doesn't answer my question, but this solution works perfectly. thanks! – preppypoof Dec 10 '13 at 22:03
  • actually, I encountered a problem. Some of the data has a space in the "id" field, so my output is `id=John Smith` instead of `id=John Smith Score=1337`. Is there a solution that accounts for this? – preppypoof Dec 10 '13 at 22:09
  • 1
    See the new Edit of my code. It work now for all your case :) – SachaDee Dec 10 '13 at 22:37
  • IT would be better to use Regex here :) `grep` or `sed` or a hybrid `batch/basic` script like [repl.bat](http://stackoverflow.com/a/16735079/2098699). – Endoro Dec 10 '13 at 22:44
  • @sachadee to make life easier and more enjoyable. – Endoro Dec 11 '13 at 01:15
  • For sure @Endoro life will be so much easier for our friend preppypoof with grep, sed or hybrid batch/basic or batch/c. Why make simple when we can complicate.... – SachaDee Dec 11 '13 at 01:24
1

The easy way:

    if not !my_line!==%%A (
     ECHO !my_line:~7,-19!>> %OUTPUTFILE%
    )   

A few issues with your code, but I applaud your effort to solve the problem.

Batch doesn't stop at a label - they're just markers, so it charges straight through, hence you'd need a

goto :eof

before any suboutine label.

You can't use numerics as metavariables, so in parse_it you'd need a letter, not 1. You can also use space as a delims character - but it must be specified as the last delimiter (ie. just before the closing "

So parse_it could be reduced (if it was required) to

for /F "tokens=1* delims= " %%q in ("%new_line%") do (
    echo %%r>> %OUTPUTFILE%
)

But - overall, bravo for the attempt!

Magoo
  • 68,705
  • 7
  • 55
  • 76
  • thank you for actually answering my question instead of giving me a different solution. I am going to use one of the other solutions instead because my input file is quite large and they are a lot faster, but I appreciate that you explained some of my mistakes. – preppypoof Dec 10 '13 at 22:12
-1

You dont say what language you want. so can you do something like:

grep IdentificationResult file | awk '{ print $3, $4}' > output.file
cianius
  • 1,926
  • 6
  • 21
  • 40