Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Finding multiple strings in a text file  (Read 9566 times)

0 Members and 1 Guest are viewing this topic.

Dias de verano

  • Guest
Re: Finding multiple strings in a text file
« Reply #15 on: May 04, 2009, 01:11:55 AM »
Batch example:

gh0std0g74's scheme for stepping back the strings to store the previous 3 lines read from source file acknowledged.

FOR line written thus to avoid (documented, apparently) problem of for /f skipping blank lines

[Details in alt.msdos.batch.nt thread headed "Bug in For /F" (long Google Groups link shortened)]: http://tinyurl.com/cwr484

I wondered why the line numbers in my output didn't match those from gh0std0g74's VBscript, but then I noticed that my batch was skipping blank lines in the input file. They match now.

Code: [Select]

@echo off
setlocal enabledelayedexpansion
set string3=
set string2=
set string1=
set i=0
set f=0

set patt3={ACAD_REACTORS
set patt2=330
set patt1=C
set patt0=102

set inputfile=dwg.txt

for /f "tokens=1* delims=]" %%a in ('find /n /v "" ^<%inputfile%') do (
set /a i+=1
set string0=%%b
if "!string0!"=="%patt0%" (
set seq=1
if "!string3!"=="%patt3%" set /a seq+=1
if "!string2!"=="%patt2%" set /a seq+=1
if "!string1!"=="%patt1%" set /a seq+=1
)
if !seq! equ 4 (
set /a f+=1

echo !string3!
echo !string2!
echo !string1!
echo !string0! [Line !i!] [!f!]
echo --------------------------

set seq=0
)
set string3=!string2!
set string2=!string1!
set string1=!string0!
)



« Last Edit: May 04, 2009, 01:35:43 AM by Dias de verano »