DosTips.com

A Forum all about DOS Batch
It is currently 04 May 2016 22:49

All times are UTC-06:00




Post new topic  Reply to topic  [ 28 posts ]  Go to page 1 2 Next
Author Message
PostPosted: 13 Jan 2012 09:55 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1635
Location: United States (east coast)
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


Top
   
PostPosted: 13 Jan 2012 10:35 
Offline

Joined: 23 Dec 2011 13:59
Posts: 3038
dbenham wrote:
  • How to search across line breaks with one search string

That might actually come in handy right away for me.


Top
   
PostPosted: 13 Jan 2012 16:25 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 1974
Location: Germany
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


Top
   
PostPosted: 19 Jan 2012 07:09 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1635
Location: United States (east coast)
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


Top
   
PostPosted: 20 Jan 2012 05:16 
Offline

Joined: 04 Nov 2011 07:59
Posts: 11
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:
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.


Top
   
PostPosted: 20 Jan 2012 07:14 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1635
Location: United States (east coast)
:shock: Wow - nice tip about a horrible "feature"

Confirmed in Vista

Dave Benham


Top
   
PostPosted: 20 Jan 2012 09:58 
Offline
Expert

Joined: 30 Aug 2007 08:05
Posts: 782
Location: Germany
Confirmed in Win7 64Bit

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

jeb


Top
   
PostPosted: 21 Jan 2012 16:31 
Offline

Joined: 23 Dec 2011 13:59
Posts: 3038
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:
**
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:
@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:
findstr /r /c:"EA .!CR!!LF!.!CR!!LF!.!CR!!LF!.!CR!!LF!GC O00" test.txt


Top
   
PostPosted: 21 Jan 2012 17:34 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 1974
Location: Germany
Try
Code:
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:
EA 9780702026171
EA 9781857152814


Regards
aGerman


Top
   
PostPosted: 21 Jan 2012 21:57 
Offline

Joined: 23 Dec 2011 13:59
Posts: 3038
Thanks aGerman!


Top
   
PostPosted: 22 Jan 2012 13:34 
Offline

Joined: 23 Dec 2011 13:59
Posts: 3038
Couple of other questions.
1) Can't seem to put this into a FOR LOOP. I get the error FINDSTR: Bad command line
Code:
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:
:: 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


Top
   
PostPosted: 22 Jan 2012 14:17 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1635
Location: United States (east coast)
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:
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


Last edited by dbenham on 27 Apr 2012 15:42, edited 1 time in total.

Top
   
PostPosted: 22 Jan 2012 14:29 
Offline

Joined: 23 Dec 2011 13:59
Posts: 3038
It's like winning the lottery every time I post a question here.
Thanks again Dave.


Top
   
PostPosted: 22 Jan 2012 15:01 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 1974
Location: Germany
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


Top
   
PostPosted: 22 Jan 2012 15:08 
Offline

Joined: 23 Dec 2011 13:59
Posts: 3038
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.


Last edited by Squashman on 22 Jan 2012 15:43, edited 1 time in total.

Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 28 posts ]  Go to page 1 2 Next

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited