Searches for regular expressions or text strings in files. Similar to grep
on Unix, but is much more limited in the regular expressions it supports.
Treats space in a regular expression as a disjunction AKA logical or unless prevented with /c
option.
Examples:
findstr /s "[0-9][0-9].*[0-9][0-9]" *.h *.cpp
Searches recursively all files whose name ends with .h
or .cpp
, printing only lines that contain two consecutive decimal digits followed by anything followed by two consecutive decimal digits.
findstr "a.*b a.*c" File.txt
Outputs all lines in File.txt
that match any of the two regular expressions separated by the space. Thus, the effect is one of logical or on regular expressions.
echo world | findstr "hello wo.ld"
findstr
treats the whole search term as a plain search term.echo world | findstr /r "hello wo.ld"
Matches. The use of /r
forces regex treatment.
findstr /r /c:"ID: *[0-9]*" File.txt
Outputs all lines in File.txt
that match the single regular expression containing a space. The use of /c
prevents the space from being treated as a logical or. The use of /r
switches the regular expression treatment on, which was disabled by default by the use of /c
. To test this, try the following:
echo ID: 12|findstr /r /c:"ID: *[0-9]*$"
Matches.
echo ID: 12|findstr /c:"ID: *[0-9]*$"
Does not match, as the search string is not interpreted as a regular expression.
echo ID: abc|findstr "ID: *[0-9]*$"
Matches despite the output of echo failing to match the complete regular expression: the search is interpreted as one for lines matching ID:
or *[0-9]*$
.
findstr /ric:"id: *[0-9]*" File.txt
Does the same as the previous example, but in a case-insensitive manner.
While findstr
enables this sort of accumulation of switches behind a single /
, this is not possible with any command. For instance, dir /bs
does not work, while dir /b /s
does.
To test this, try the following:
echo ID: 12|findstr /ric:"id: *[0-9]*$"
echo ID: ab|findstr /ric:"id: *[0-9]*$"
findstr /msric:"id: *[0-9]*" *.txt
Like above, but recursively for all files per /s, displaying only matching files rather than matching lines per /m.
echo hel lo | findstr /c:"hel lo" /c:world
/c
switch can be used multiple times to create logical or.
echo \\hello\\ | findstr "\\hello\\"
Does not match. Backslash before quotation marks and multiple other characters acts as an escape; thus, \" matches ".
echo \\hello\\ | findstr "\\\\hello\\\\"
Matches. Double backslash passed to findstr
stands for a single backslash.
echo \\hello\\ | findstr \\hello\\
Matches. None of the single backslashes passed to findstr
is followed by a character on which the backslash acts as an escape.
echo ^"hey | findstr \\^"hey | more
To search for a quote (quotation mark), you need to escape it two times: once for the shell using caret (^), and once for findstr
using backslash (\\
).
echo ^"hey | findstr ^"\\^"hey there^" | more
To search for a quote and have the search term enclosed in quotes as well, the enclosing quotes need to be escaped for the shell using caret (^
).
echo //comment line | findstr \\//