problems with FINDSTR in multiline search

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ralfs_k
Posts: 6
Joined: 24 Feb 2012 04:17

problems with FINDSTR in multiline search

#1 Post by ralfs_k » 24 Feb 2012 04:23

hi I need to create batch file that searches in text file following string:

Code: Select all

012345;
;
;
;


but this doesn't work!

Code: Select all

setlocal
set LF=^


setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
findstr /r /i "[0-9]*!LF!;!LF!;!LF!;!LF!;" < %%a > nul
if not errorlevel 1 echo delete >> %%a
)
pause > nul

any ideas?

jeb
Expert
Posts: 1059
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: problems with FINDSTR in multiline search

#2 Post by jeb » 24 Feb 2012 04:50

I would recommend to read the unofficial but perfect findstr-bug&feature documentation of dbenham
What are the undocumented features and limitations of the Windows FINDSTR command?

I suppose you will change to grep then :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: problems with FINDSTR in multiline search

#3 Post by foxidrive » 24 Feb 2012 06:09

Try this. The upper section is file.txt used by the batch file in a simple test.

Code: Select all

a
b
c
012345;
;
;
;
d
e
f
012345;
;
;
;

012345;
;
;
;
012345;
;
;
;


Code: Select all

@echo off
set a=
setlocal EnableExtensions EnableDelayedExpansion
for /f "eol=! delims=" %%a in (file.txt) do (
echo %%a
if defined a (
if "%%a"==";" (set /a num=!num!+1) else (set a=)
if !num! EQU 3 (
echo found line sequence
set num=
set a=
)
)
if "%%a"=="012345;" set a=1
)
pause

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: problems with FINDSTR in multiline search

#4 Post by dbenham » 24 Feb 2012 06:24

@ralfs_k - It failed because you didn't account for the <CR> characters in the Windows style line terminator. A regex of "<CR>*<LF>" will match both unix and Windows style line terminators. Read the link that jeb posted for more info.

Dave Benham

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: problems with FINDSTR in multiline search

#5 Post by Squashman » 24 Feb 2012 08:30

Your search parameter also has another fundamental flaw.
You are searching for any amount of numbers, then a LF and then a semi colon but your example is number semicolon and then there would be a LF if your file is unix based.

Post Reply