Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Findstr's command-line analysis mechanism


May 23, 2021 DOS Command learning manual



Source: http://www.cn-dos.net/forum/viewthread.php?tid=21167&fpage=1&highlight=findstr

The problem began with bsijl's topic of findstr error filtering, which, because the cause could not be found, could only be speculated that there was something wrong with the /g switch.


Recently, because of writing a debuger code and using findstr/g again, the result was the same problem. A fter careful testing, it was found to be findstr

Problems caused by special command-line analysis mechanisms. Here's a brief description:


1, findstr is different from the earlier find, it has certain requirements for the arrangement of parameters, that is, to follow the switch (omittable), string (omitted when using /c switch), file name (can be matched, can be multiple, there is input flow to be omitted) order.


2, the switch can use quotation marks, so you can not directly search with the same keywords as the switch;


3, the search for keywords on the situation and performance is more complex, as follows:

3-1, whether or not the switch /l or/r is used and whether or not quotation marks are used, the keyword will become an escape character, so the "will become", "will make the quotation marks lose the role of string definition;

3-2, search keywords can be added or not quoted, when quotation marks, which may be escaped again. The escape results when using switches/l and/r may be different: when you use /r, the " becomes a single, " will make the keyword empty, and when used/l or not used, "" and "" and "" will be equivalent to a single.

3-3, if the keyword ends in a single , and there are no quotation marks, it will not be escaped;


The problem with findstr/g should be related to the above, but its internal rationale is still not fully understood. I wonder what you think?


Batch removal XP input method problem! Please dos master to solve

http://www.cn-dos.net/forum/search.php?searchid=24472


[2] Test of findstr/v/g



Quote:

E:\Batch\Test>set > envar.out


E:\Batch\Test>findstr /v /g:envar.out envar.out

LOGONSERVER=\\Test

ProgramFiles=C:\Program Files


E:\Batch\Test>findstr /v /i /g:envar.out

envar.out

LOGONSERVER=\\Test




[3] Test of findstr

CODE: [Copy to clipboard]



:: Test of findstr

:: Will Sort - 2006-06-10 - CMDWinXP

@echo off

cls&echo ---- "set>_tfs1.tmp & findstr /v

/g:_tfs1.tmp _tfs1.tmp"

set>_tfs1.tmp & findstr /v /g:_tfs1.tmp

_tfs1.tmp

pause


echo ---- "findstr /v /i /g:_tfs1.tmp _tfs1.tmp"

findstr /v /i /g:_tfs1.tmp _tfs1.tmp

pause


echo ---- "sort /r _tfs1.tmp > _tfs2.tmp &

findstr /v /g:_tfs1.tmp _tfs2.tmp"

sort /r _tfs1.tmp > _tfs2.tmp & findstr /v

/g:_tfs1.tmp _tfs2.tmp

pause


echo ---- "set|findstr /v /g:_tfs1.tmp"

set|findstr /v /g:_tfs1.tmp

pause


cls&echo ---- "dir C:\ /w > _tfs2.tmp & findstr

/v /g:_tfs2.tmp _tfs2.tmp"

dir C:\ /w > _tfs2.tmp & findstr /v /g:_tfs2.tmp

_tfs2.tmp

pause


echo ---- "echo :\ > _tfs2.tmp & findstr /v

/g:_tfs2.tmp _tfs2.tmp"

echo :\ > _tfs2.tmp & findstr /v /g:_tfs2.tmp

_tfs2.tmp

pause


cls&echo ---- "findstr /g:_tfs1.tmp

_tfs1.tmp>_tfs2.tmp & fc _tfs1.tmp _tfs2.tmp"

findstr /g:_tfs1.tmp _tfs1.tmp>_tfs2.tmp & fc

_tfs1.tmp _tfs2.tmp

pause


cls&echo ---- "set|findstr /r "\\\\ \\\\""

set|findstr /r "\\\\ \\\\"

pause


echo ---- "set|findstr /l "\\\\ \\\\""

set|findstr /l "\\\\ \\\\"

pause


cls&echo ---- "echo _tfs1_tmp > _tfs1.tmp &

findstr "/l" "_tfs1.tmp" "_tfs1.tmp""

echo _tfs1_tmp >> _tfs1.tmp & findstr

"_tfs1.tmp" "_tfs1.tmp"

pause

del _tfs?. Tmp

[ Last edited by willsort on 2006-6-11 at 18:38 ]





Climbing

Gold membership


"2nd Floor":


How does this result explain on my XP?


d:\work>findstr /v /g:envar.out envar.out

FINDSTR: The search string is too long.


d:\work>findstr /v /g:envar.out envar.out

FINDSTR: The search string is too long.


d:\work>findstr /v /g:envar.out

FINDSTR: The search string is too long.



Helpless

Moderator


3rd Floor:


Under my XP, it's the same description as willsort Brother 3. T his annoying result doesn't know if you can be sure it is

FINDSTR BUG, ask your friends to try out more about other systems. L ooks like the keyword contains the word """"

Characters should be more careful.



3742668

Moderator


"4th floor":


Well, it seems that errors occur only when the characters that follow are non-letters and non-numbers. It feels like Microsoft is ready to make findstr into a regular expression tool under cmd, but it doesn't take into account compatibility with other parameters, resulting in the end result that the tiger is not an anti-dog.

In addition, at some point, findstr's lookup string contains Chinese, you need to add /i parameters to avoid errors, the specific environment and code has been forgotten, perhaps findstr in addition to the regular expression aspects of the bug, there are unicode and ascii conversion bugs.



willsort

Moderator


"5th floor":


Re Ups:


Another question:


Because the switch also allows quotation marks, it cannot be used as "/l" or "/r"

When the same string of text as the switch matches as a keyword, the following sentence will go wrong:

echo /l /r > test

findstr /l "/r" test


However, you can use the switch /c to force a keyword to be specified:

findstr /l /c:"/r" test

findstr /l /c:/r test


There's another way, and that's what's mentioned above:

findstr /l \/r test

In addition, this can also allow our keywords to include quotation marks:

echo /l /r >test

echo "/r" >> test

findstr /l \"/r\" test


Finally, revise and add some of the descriptions in Top Floor 3-2:

3-2, switch/r and switch/l are the same, the following keywords can be used or not used quotation marks;

3-2-1, " and """" are equivalent to quotation marks and the number of keywords represented by each string thereafter;

3-2-2, """""""

3-2-3, "

3-2-4、"\ ","\\","\\\ ","\\\\\

","

3-2-5, """

3-2-6, """

3-2-7, the equivalent of "

3-2-8, "


The above data are measured by the following methods, the test file can be found in the test file, when the do not use / r switch and use switch / l the same, ;

type test1.txt | f indstr /r "test_key"

test2.xt



[1] Test text of findstr - "test1.txt"



Quote:

/l /r

"/l"

" test1

\" test1

\\" test1

\\\" test1

\\\\" test1

\ test1

\\ test1

\\\ test1

\\\\ test1




[2] Test text of findstr - "test2.txt"



Quote:

" test2

\" test2

\\" test2

\\\" test2

\\\\" test2

\ test2

\\ test2

\\\ test2

\\\\ test2




[ Last edited by willsort on 2006-6-11 at 18:25 ]




220110

Moderator



"6th floor":





Quote:

3-2, switch/r and switch/l are the same, the following keywords can be used or not used quotation marks;

3-2-1, " and """" are equivalent to quotation marks and the number of keywords represented by each string thereafter;

3-2-2, """""""

3-2-3, "

3-2-4、"\ ","\\","\\\ ","\\\\\

","

3-2-5, """

3-2-6, """

3-2-7, the equivalent of "

3-2-8, "




I also found earlier that this law exists on the path name, but did not study in depth, did not dare to put it on.

I'm from "dir .", " dir . . . At first, you might as well test it.



willsort

Moderator


Status Offline "7th Floor":


Re 220110:


UNC path name in the resolution and findstr

Keywords are different, they only define the role of the directory, not the role of escape characters.



In addition, in the findstr /v%var%% test.txt statement, if the last line of test.txt is to match %var, and the line is not empty, the contents of the last line cannot be filtered, as if the branch flag identifies an error