Page 1 of 2

Undocumented FINDSTR features and limitations

Posted: 13 Jan 2012 09:55
by dbenham
For those that are interested, I've posted an exhaustive list of undocumented FINDSTR features and limitations at StackOverflow.

A few of the more interesting tid-bits
  • Line length limits for piped or redirected input
  • Escape rules for " and \ within search strings
  • Which characters do not work properly within search strings
  • How exactly does FINDSTR determine what is a line
  • How to search across line breaks with one search string
  • Precise explanation of regex terms ^ $ \< \> and [x-y]


Dave Benham

Re: Undocumented FINDSTR features and limitations

Posted: 13 Jan 2012 10:35
by Squashman
dbenham wrote:
  • How to search across line breaks with one search string

That might actually come in handy right away for me.

Re: Undocumented FINDSTR features and limitations

Posted: 13 Jan 2012 16:25
by aGerman
Thanks Dave. I wasn't aware of some of these issues.

BTW I appreciate FINDSTR for supporting regular expressions, but I hate it for not returning the matched sub string but the entire line instead :evil:

Regards
aGerman

Re: Undocumented FINDSTR features and limitations

Posted: 19 Jan 2012 07:09
by dbenham
FINDSTR BUG ALERT :!:

FINDSTR may give the wrong answer if multiple literal search strings are specified unless the /I option is used. See Why doesn't this FINDSTR example with multiple literal search strings find a match? for more info.

Also, the default type of search (Literal vs Regex) is more complicated than I thought. I've updated my link in the first post of this thread.


Dave Benham

Re: Undocumented FINDSTR features and limitations

Posted: 20 Jan 2012 05:16
by Judago
There is a pretty serious bug that is present on xp(haven't tested others). Using more than 15 classes doesn't just result in a text(in console) error message, but the typical windows error popup.

Code: Select all

echo 01234567890123456|findstr [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]


Find String (QGREP) Utility has encountered a problem and needs to close. We are sorry for the inconvenience.

Re: Undocumented FINDSTR features and limitations

Posted: 20 Jan 2012 07:14
by dbenham
:shock: Wow - nice tip about a horrible "feature"

Confirmed in Vista

Dave Benham

Re: Undocumented FINDSTR features and limitations

Posted: 20 Jan 2012 09:58
by jeb
Confirmed in Win7 64Bit

That's the problem of Microsoft, some components are written by drug consuming apprentices.

jeb

Re: Undocumented FINDSTR features and limitations

Posted: 21 Jan 2012 16:31
by Squashman
So can the search be extended across multiple lines?
I am basically looking at the lines that start with EA and 4 lines down will be the line GC.

Code: Select all

**
YP 2001
RC U
EA 9780702026171
RF R
WE 1780
SG 1
GC O00
I3 1313101313134
PC S6.1T
**
YP 2003
RC J
EA 9780237526306
RF R
WE 220
SG 4
GC C09
I3 1111232349544
PC Y2.1
**
YP 2005
RC G
EA 9781857152814
RF R
WE 562
SG 3
GC O00
I3 1483859583943
PC S6.3Y
**

So I want to pull out the EA line when the GC line Equals GC O00
So I gave this a try.

Code: Select all

@echo off
setlocal
::Define LF variable containing a linefeed (0x0A)
set LF=^


::Above 2 blank lines are critical - do not remove

::Define CR variable containing a carriage return (0x0D)
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

setlocal enableDelayedExpansion
::regex "!CR!*!LF!" will match both Unix and Windows style End-Of-Line
findstr  /r /c:"EA *!CR!*!LF!*!CR!*!LF!*!CR!*!LF!*!CR!*!LF!GC O00" test.txt

no go on this as well.

Code: Select all

findstr /r /c:"EA .!CR!!LF!.!CR!!LF!.!CR!!LF!.!CR!!LF!GC O00" test.txt

Re: Undocumented FINDSTR features and limitations

Posted: 21 Jan 2012 17:34
by aGerman
Try

Code: Select all

findstr  /r /c:"EA ..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!GC O00" test.txt

. matches exactly 1 character
.* matches zero or more characters
..* matches min. 1 or more characters

My output for your test.txt is

Code: Select all

EA 9780702026171
EA 9781857152814


Regards
aGerman

Re: Undocumented FINDSTR features and limitations

Posted: 21 Jan 2012 21:57
by Squashman
Thanks aGerman!

Re: Undocumented FINDSTR features and limitations

Posted: 22 Jan 2012 13:34
by Squashman
Couple of other questions.
1) Can't seem to put this into a FOR LOOP. I get the error FINDSTR: Bad command line

Code: Select all

FOR /F "tokens=2 delims= " %%A in ('findstr /r /c:"EA ..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!GC O00" test.txt') do echo %%A


2)Dave said in his article that using the /G option is imprecise. Well I actually need to search for several GC variables in the file.
GC O00 GC K02 GC Q00 GC F05 GC C00
Could I make each of the search strings a variable so that I can shorten up the actual findstr command in the for loop. Do I have to escape the exclamations in the CR and LF variables so that they don't expand when setting them to a variable?

Code: Select all

:: Is this correct?
SET "S1=EA ..*^!CR^!*^!LF^!..*^!CR^!*^!LF^!..*^!CR^!*^!LF^!..*^!CR^!*^!LF^!GC O00"
:: Or is this OK?
SET "S2=EA ..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!GC K02"
SET "S3=EA ..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!GC Q00"
SET "S4=EA ..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!GC F05"
SET "S5=EA ..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!GC C00"
FOR /F "tokens=2 delims= " %%A in ('findstr /r /c:"%S1%" /c:"%S2%" /c:"%S3%" /c:"%S4%" /c:"%S5%" test.txt') do echo %%A

Re: Undocumented FINDSTR features and limitations

Posted: 22 Jan 2012 14:17
by dbenham
The IN() clause is run in it's own CMD session. The CR and LF must be expanded within that session. Each ! must be escaped so that the variables are not expanded in the parent batch session. But the IN() clause CMD session does not inherit the Delayed Expansion state, so you need to use CMD /V:ON.

I think this will work (untested). Edit - fixed bug - %A became %%A as per aGerman's post below

Code: Select all

set "prefix=EA ..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!..*!CR!*!LF!GC "
set "search="
for %%A in (O00 K02 Q00 F05 C00) do set "search=!search! /c:"!prefix!%%A"
FOR /F "tokens=2 delims= " %%A in ('cmd /v:on /c "findstr /r ^!search^! test.txt"') do echo %%A


Dave Benham

Re: Undocumented FINDSTR features and limitations

Posted: 22 Jan 2012 14:29
by Squashman
It's like winning the lottery every time I post a question here.
Thanks again Dave.

Re: Undocumented FINDSTR features and limitations

Posted: 22 Jan 2012 15:01
by aGerman
I had to correct this line to get it to run:
for %%A in (O00 K02 Q00 F05 C00) do set "search=!search! /c:"!prefix!%%A""

Regards
aGerman

Re: Undocumented FINDSTR features and limitations

Posted: 22 Jan 2012 15:08
by Squashman
aGerman wrote:I had to correct this line to get it to run:
for %%A in (O00 K02 Q00 F05 C00) do set "search=!search! /c:"!prefix!%%A""

Regards
aGerman

Yeah, I saw that as well when I initially ran the code. Didn't want to discredit Dave for a minor syntax error.