问题
May I know what is the differences between 2 findstr code below?
First Case:
findstr /m 0632 log_network.txt
if %errorlevel%==0 (
echo FOUND
) else (
echo NOT FOUND
)
Second Case:
set entire_line="0632"
echo %entire_line% | findstr /m log_network.txt
if %errorlevel%==0 (
echo FOUND
) else (
echo NOT FOUND
)
The first case return "FOUND" and the second case return "NOT FOUND"... Also, i always see people like to use:
echo %something% | findstr /m filename.txt > null
But I don't understand why they write in this way...
log_network.txt Content:
Set_Param_10A "TRUE" "xnetwork.exist.5846"
Set_Param_10A "TRUE" "xnetwork.exist.7425"
Set_Param_10A "TRUE" "xnetwork.exist.1420"
Set_Param_10A "TRUE" "xnetwork.exist.0632"
Set_Param_10A "TRUE" "xnetwork.exist.1112"
Set_Param_10A "TRUE" "xnetwork.exist.8524"
Set_Param_10A "TRUE" "xnetwork.exist.3675"
Set_Param_10A "TRUE" "xnetwork.exist.3344"
Set_Param_10A "TRUE" "xnetwork.exist.1276"
Set_Param_10A "TRUE" "xnetwork.exist.4796"
Set_Param_10A "TRUE" "xnetwork.exist.3349"
Set_Param_10A "TRUE" "xnetwork.exist.0048"
Thanks...
回答1:
First case is searching the string 0632
into the file log_network.txt
and if it is found in the file, the name of the file will be echoed to console (/m
switch). If found, errorlevel will be 0, if not found, errorlevel will be 1.
Second case is searching the string log_network.txt
in the data it receives from stdin, that is, "0632"
. This is probably an error or a misinterpretation on the way findstr works.
To "mimic" the functionality of the first code, but using the structure of the second, it should be something like
set "entire_line=0632"
echo %entire_line%| findstr /g:/ /m log_network.txt
That is, first remove the quotes from the searched value (in the original code they are included in the value), and then ask findstr to take search strings from stdin (/g:/
), check them against the content of the file log_network.txt
and output the name of the file if any coincidence is found (/m
)
In any case, it is a lot more efficient the first construct. Creating a pipe involves more cpu usage than just leaving findstr
directly do the work.
来源:https://stackoverflow.com/questions/24136579/what-is-the-differences-between-2-findstr-code-below