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 \\//
If forward slash (/
) is the 1st character in the search term, it needs to be escaped with a backslash (\). The escaping is needed even if the search term is enclosed in quotes.
findstr /f:FileList.txt def.*():
Search in the files stated in FileList.txt
, one file per line. File names in FileList.txt
can contain spaces and do not need to be surrounded with quotation marks for this to work.
findstr /g:SearchTermsFile.txt *.txt
Search for the search terms found in SearchTermsFile.txt
, one search term per line. A space does not serve to separate two search terms; rather, each line is a complete search term. A line is matched if at least one of the search terms matches. If the first search term looks like a regex, the search will be a regex one, but if it looks like a plain search term, the whole search will be a plain one even if 2nd or later search terms look like regex.
findstr /xlg:File1.txt File2.txt
Outputs set intersection: lines present in both files.