What are the undocumented features and limitations of the Windows FINDSTR command?

后端 未结 8 1687
耶瑟儿~
耶瑟儿~ 2020-11-21 04:23

The Windows FINDSTR command is horribly documented. There is very basic command line help available through FINDSTR /?, or HELP FINDSTR, but it is

8条回答
  •  终归单人心
    2020-11-21 05:27

    When several commands are enclosed in parentheses and there are redirected files to the whole block:

    < input.txt (
       command1
       command2
       . . .
    ) > output.txt
    

    ... then the files remains open as long as the commands in the block be active, so the commands may move the file pointer of the redirected files. Both MORE and FIND commands move the Stdin file pointer to the beginning of the file before process it, so the same file may be processed several times inside the block. For example, this code:

    more < input.txt >  output.txt
    more < input.txt >> output.txt
    

    ... produce the same result than this one:

    < input.txt (
       more
       more
    ) > output.txt
    

    This code:

    find    "search string" < input.txt > matchedLines.txt
    find /V "search string" < input.txt > unmatchedLines.txt
    

    ... produce the same result than this one:

    < input.txt (
       find    "search string" > matchedLines.txt
       find /V "search string" > unmatchedLines.txt
    )
    

    FINDSTR is different; it does not move the Stdin file pointer from its current position. For example, this code insert a new line after a search line:

    call :ProcessFile < input.txt
    goto :EOF
    
    :ProcessFile
       rem Read the next line from Stdin and copy it
       set /P line=
       echo %line%
       rem Test if it is the search line
       if "%line%" neq "search line" goto ProcessFile
    rem Insert the new line at this point
    echo New line
    rem And copy the rest of lines
    findstr "^"
    exit /B
    

    We may make good use of this feature with the aid of an auxiliary program that allow us to move the file pointer of a redirected file, as shown in this example.

    This behavior was first reported by jeb at this post.


    EDIT 2018-08-18: New FINDSTR bug reported

    The FINDSTR command have a strange bug that happen when this command is used to show characters in color AND the output of such a command is redirected to CON device. For details on how use FINDSTR command to show text in color, see this topic.

    When the output of this form of FINDSTR command is redirected to CON, something strange happens after the text is output in the desired color: all the text after it is output as "invisible" characters, although a more precise description is that the text is output as black text over black background. The original text will appear if you use COLOR command to reset the foreground and background colors of the entire screen. However, when the text is "invisible" we could execute a SET /P command, so all characters entered will not appear on the screen. This behavior may be used to enter passwords.

    @echo off
    setlocal
    
    set /P "=_" < NUL > "Enter password"
    findstr /A:1E /V "^$" "Enter password" NUL > CON
    del "Enter password"
    set /P "password="
    cls
    color 07
    echo The password read is: "%password%"
    

提交回复
热议问题