FINDSTR
suggest changeSearches 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"
- Does not match. Since the 1st item before the space does not look like a regex,
findstr
treats the whole search term as a plain search term.
- Does not match. Since the 1st item before the space does not look like a regex,
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, whiledir /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 inFileList.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.
findstr /xlvg:File2.txt File1.txt
Outputs set difference:
File1.txt
-File2.txt
.findstr /m Microsoft C:\Windows\system32\*.com
Works with binary files no less than text files.
Limitations of the regular expressions of findstr
, as compared to grep
:
- No support of groups -- "\(", "\)".
- No support of greedy iterators -- "*?".
- No support of "zero or one of the previous" -- "?".
- And more.
Bugs:
echo bb|findstr "bb baaaa"
Does not find anything in multiple Windows versions, but it should.
Links:
- findstr at ss64.com
- findstr at Microsoft
- What are the undocumented features and limitations of the Windows FINDSTR command?
$ findstr /?
Searches for strings in files.
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
[/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
strings [[drive:][path]filename[ ...]]
/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all
subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename
Specifies a file or files to search.
Use spaces to separate multiple search strings unless the argument is prefixed
with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.
Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurrences of previous character or class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[x-y] Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\<xyz Word position: beginning of word
xyz\> Word position: end of word
For full information on FINDSTR regular expressions refer to the online Command
Reference.