findstr slash not working a expected

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gunitinug
Posts: 9
Joined: 09 Nov 2017 20:04

findstr slash not working a expected

#1 Post by gunitinug » 23 Nov 2017 17:19

Code: Select all

c:\test batch scripts\errorlevel demo>echo "a b c /" | findstr /r "//"
FINDSTR: // ignored
FINDSTR: Bad command line
The process tried to write to a nonexistent pipe.
I was expecting output

Code: Select all

a b c /
THX

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: findstr slash not working a expected

#2 Post by penpen » 23 Nov 2017 20:15

A not well known fact is, that you could surround each command line token (such as command line arguments) with doublequotes.
The findstr command interprets (like many other tools) anything starting with a slash as a command line argument (no matter if it is between two doublequotes or not);
it can't know if the next is meant to be a search strings option or not.
And because of the fact that there is no command line parameter or option with the name "//", you get this error message.

I also think you have mistaken the slash ('/') with the backslash ('\') character:
The slash is no special character within a findstr search string, so in common you don't need to escape it (especially not with a second slash).

But in this case escaping the slash is one possibility to get what you want (no matter if you are using doublequotes or not):

Code: Select all

Z:\>echo "a b c /" | findstr "/r" "\/"
"a b c /"

Z:\>echo a b c / | findstr /r \/
a b c /
Another solution is this:

Code: Select all

Z:\>echo "a b c /" | findstr /r ".*/"
"a b c /"
(Just avoid that the slash character to be the first character.)

penpen

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

Re: findstr slash not working a expected

#3 Post by dbenham » 23 Nov 2017 21:48

The simplest solution is to use the /C option, which forces FINDSTR to recognize the argument as a search string instead of an option.

The /C option defaults to a literal search, but the /R option overrides the behavior and forces a regular expression (not an issue with this search)

Code: Select all

echo "a b c /" | findstr /r /c:/
:: or
echo "a b c /" | findstr /r /c:"/"
Another option is to use two search strings, just make sure the first string can never match, and does not start with /
But this solution obscures the intent, so I don't really like it.

Code: Select all

echo "a b c /" | findstr /r "$^ /"
Dave Benham

Post Reply