You are not logged in.

#1 15 Apr 2020 22:10

lazna
Member
Registered: 15 Apr 2020
Posts: 12

findstr - pattern or parameter

Dealing with curl utility, trying to read about its parameters '-o' performing:

curl -h | findstr  "-o"
FINDSTR: Bad command line

curl probably expect there is a (wrong) parameter doublequoted. Find two workarounds:

curl -h | findstr /C:"-o"

and

curl -h | findstr "\-o"

first question is: Is this known issue? Did not find it described.... Second question: which workaround is better?
Running W10.

Offline

#2 16 Apr 2020 06:35

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: findstr - pattern or parameter

An undocumented (although mentioned on https://ss64.com/nt/findstr.html) feature of findstr is that it takes options in both the traditional Windows style (/option) and the Unix style (-option). Since /O is a valid option, findstr is reading -o as the flag to print the character offset before each matching line, but then because it's being read in as an option, findstr incorrectly believes that no search string is being provided and throws that error.

Your first workaround is the preferred method, as /C should always be specified whenever possible.

Last edited by Shadow Thief (16 Apr 2020 06:38)

Offline

#3 16 Apr 2020 09:16

lazna
Member
Registered: 15 Apr 2020
Posts: 12

Re: findstr - pattern or parameter

Shadow Thief wrote:

An undocumented (although mentioned on xxx feature of findstr is that it takes options in both the traditional Windows style (/option) and the Unix style (-option). Since /O is a valid option, findstr is reading -o as the flag to print the character offset before each matching line, but then because it's being read in as an option, findstr incorrectly believes that no search string is being provided and throws that error.

My question in "known issue" mean how findstr distinguish between paramater and pattern to search. Expect that the doublequoted string is treated a s pattern to search. Do findstr parse every string for possibly valid option and consider it is a pattern only if valid option does not found there?

Offline

#4 16 Apr 2020 16:21

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: findstr - pattern or parameter

I didn't find anything in https://stackoverflow.com/questions/884 … str-comman, but then I thought that this might be a feature of cmd.exe itself, so I read through https://stackoverflow.com/questions/409 … se-scripts. I believe the quotes get stripped during the tokenization part of phase 2 because they don't have any special meaning to findstr, unlike find, which requires quotes.

Offline

#5 17 Apr 2020 09:40

lazna
Member
Registered: 15 Apr 2020
Posts: 12

Re: findstr - pattern or parameter

Comparing:

echo hallo world| findstr "hallo" "world"
FINDSTR: Cannot open world

echo hallo world| findstr hallo world
FINDSTR: Cannot open world

echo hallo world| findstr "hallo world"
hallo world

look like findstr eat first parameter as a pattern, than everything else is considered as a filenames. It does not support dash as a identificator the datasource come from stdin instead of file, as many other programs.

Offline

#6 18 Apr 2020 10:40

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: findstr - pattern or parameter

Findstr takes arguments in the order

findstr flags search_string file_name

, so in your first two examples there, you are only providing "hello" as the search string and since world is being passed in as the thing to search, the pipe is ignored.

Offline

Board footer

Powered by