I just want to understand how FINDSTR
works. For example I have the following (test) batch script:
我只是想了解FINDSTR是如何工作的。例如,我有以下(测试)批处理脚本:
set someText=hi
echo %sometext% | findstr /i /c:"hello"
if %errorlevel%==0 (
echo PASSED
) else (
echo NOT PASSED
)
pause
In this example, if someText
contains "hello", for example "Hello, world!", the output would be:
在此示例中,如果someText包含“hello”,例如“Hello,world!“,输出将是:
Hello, world!
PASSED
But if someText
does not contain "hello", it will simply output:
但是,如果某个文本不包含“Hello”,它将简单地输出:
NOT PASSED
Why? Where did my echo %someText%
go? And how to bring it back?
为什么?我的回音到哪里去了?以及如何把它带回来?
更多回答
the ouput of echo
is filtered by findstr
. It does only display matching strings.
回声的输出由findstr过滤。它只显示匹配的字符串。
The question is answered by the Microsoft documentation about Using command redirection operators. The output of ECHO to handle STDOUT (standard output) of cmd
is redirected by cmd
to handle STDIN (standard input) of the executable %SystemRoot%\System32\findstr.exe
run by cmd
after command ECHO by calling the Windows kernel library function CreateProcess.
有关使用命令重定向操作符的Microsoft文档回答了这个问题。通过调用Windows内核库函数CreateProcess,cmd在回显命令后,将cmd处理STDOUT(标准输出)的输出重定向到处理由cmd运行的可执行文件%SystemRoot%\System32\findstr.exe的STDIN(标准输入)。
The space left to |
is also output by the cmd
internal command ECHO because of ECHO does not interpret a space as argument string delimiter as most Windows commands do. CD is another command which does not interpret a space as argument string delimiter. IT would be necessary using echo %sometext%| findstr /i /c:"hello"
for avoiding the output of a trailing space which would be important on using set someText=hi
and echo %sometext%| findstr /i /x /c:"hi"
; note option /x
.
由于ECHO不像大多数Windows命令那样将空格解释为参数字符串分隔符,因此cmd内部命令ECHO也会输出留到|的空格。Cd是另一个不将空格解释为参数字符串分隔符的命令。为了避免输出尾随空格,必须使用ECHO%sometext%|findstr/i/c:“Hello”,这对于使用set ome Text=hi和ECHO%sometext%|findstr/i/x/c:“hi”;注意选项/x非常重要。
echo %sometext% | findstr /i /x /c:"hi"
would not result in a positive match as the entire line is not matched by the search string hi
because of the trailing space output by echo
too.
ECHO%sometext%|findstr/i/x/c:“hi”不会导致正匹配,因为由于ECHO输出的尾随空格也导致整行与搜索字符串hi不匹配。
if %errorlevel%==0 (
should be if not errorlevel 1
as described by the help output on running if /?
in a command prompt window because of this syntax works always even within a command block beginning with (
and ending with matching )
. There could be used also echo %sometext%| %SystemRoot%\System32\findstr.exe /i /c:"hello" && echo FOUND|| echo NOT FOUND
. See also: Single line with multiple commands using Windows batch file.
如果%errorLevel%==0(如果不是错误,则应为1,如运行if/?时的帮助输出所述)。在命令提示符窗口中,由于这种语法,即使在以开头(和以匹配结束)的命令块中也始终有效。还可以使用ECHO%sometext%|%SystemRoot%\System32\findstr.exe/i/c:“Hello”&&ECHO FOUND||ECHO NOT FOUND。另请参阅:使用Windows批处理文件的单行多命令。
优秀答案推荐
The "|"
in your command passed standard output from the echo
command to the standard input of the findstr
command.
命令中的“|”将ECHO命令的标准输出传递给findstr命令的标准输入。
The output "Hello, world!" seen when matched is the standard output from the findtr
command when matched. It is not the output from the echo
command
输出“Hello,world!”匹配时显示的是匹配时findtr命令的标准输出。它不是ECHO命令的输出
findstr
filters your text (shows matching lines and suppresses non-matching lines). Nothing you can do about that, so you need another approach. Redirect the findstr
output to NUL
("nirwana") so you have a consistent behaviour with matches and non-matches. Then echo the string again:
Findstr过滤文本(显示匹配的行并取消不匹配的行)。你对此无能为力,所以你需要另一种方法。将findstr输出重定向到nul(“nirwana”),这样您就可以对匹配和非匹配保持一致的行为。然后再次回显字符串:
set someText=hi
echo %sometext% | findstr /i /c:"hello" >nul
echo %sometext%
if %errorlevel%==0 (
echo PASSED
) else (
echo NOT PASSED
)
pause
更多回答
is there any way to execute the echo
command or whatever command is before findstr
first, then pass the output to findstr
?
有没有办法先执行ECHO命令或findstr之前的任何命令,然后将输出传递给findstr?
@ItsSuperPlayz: Just execute echo %sometext%
alone before the line with the findstr
...
@ItsSuperPlayz:只需在findstr行之前单独执行echo %sometext%即可。
我是一名优秀的程序员,十分优秀!