Page 1 of 1
problems with FINDSTR in multiline search
Posted: 24 Feb 2012 04:23
by ralfs_k
hi I need to create batch file that searches in text file following string:
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?
Re: problems with FINDSTR in multiline search
Posted: 24 Feb 2012 04:50
by jeb
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

Re: problems with FINDSTR in multiline search
Posted: 24 Feb 2012 06:09
by foxidrive
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
Re: problems with FINDSTR in multiline search
Posted: 24 Feb 2012 06:24
by dbenham
@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
Re: problems with FINDSTR in multiline search
Posted: 24 Feb 2012 08:30
by Squashman
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.