批处理以解析包含输入文件中特定字符串的行

时间:2013-12-10 20:40:30

标签: batch-file

编辑:是的,这必须批量完成。

我需要能够读取输入文件,并解析出只包含特定字符串的行的某些部分,然后将其写入输出文件。例如:

输入=

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

在包含字符串“IdentificationResult”的行中,我需要返回包含id和Score的字符串。

预期产出=

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

这是我到目前为止所做的:

@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%
)

我现在遇到的问题是,当我运行此脚本时,我得到')此时意外错误。当我从输入中删除括号时,我得到了预期的结果。我已经尝试包含如下所示的行来删除括号:

:: 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%
)

我知道在我想要的行中,带括号的部分总是正好是18个字符,所以我从最后修剪它们。但是,当我这样做时,由于某种原因,我得到以下作为我的输出:

输出错误:

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

所以,我只得到我要解析的第一行的数据,而且我得到了三次(即使我的输入中只有两行符合我的标准)。为什么我多次获取此数据而不是正确的数据?另外,还有一种更好的方法可以解决')此时出乎意料的错误吗?

4 个答案:

答案 0 :(得分:1)

编辑修改后没有尾随空格。没有“ScoreAdjustment”并与“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

答案 1 :(得分: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

答案 2 :(得分:1)

简单方法:

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

您的代码存在一些问题,但我对您解决问题的努力表示赞赏。

批次不会停留在标签上 - 它们只是标记,因此直接收费,因此您需要

goto :eof

在任何子标签之前。

您不能将数字用作元变量,因此在parse_it中您需要一个字母,而不是1。您还可以将空格用作delims字符 - 但必须将其指定为最后一个分隔符(即在结束之前

因此parse_it可以减少(如果需要的话)

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

但是 - 整体而言,勇敢地尝试!

答案 3 :(得分:-1)

你不会说你想要的语言。所以你可以这样做:

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