preppypoof
preppypoof

Reputation: 35

Batch to parse lines containing a specific string from an input file

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?

Upvotes: 3

Views: 11475

Answers (4)

SachaDee
SachaDee

Reputation: 9545

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

Upvotes: 1

Magoo
Magoo

Reputation: 80023

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!

Upvotes: 1

Endoro
Endoro

Reputation: 37569

@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

Upvotes: 1

cianius
cianius

Reputation: 2412

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

grep IdentificationResult file | awk '{ print $3, $4}' > output.file

Upvotes: -1

Related Questions