Полезное‎ > ‎CMD‎ > ‎

findstr

FINDSTR

Search for strings in files.

Syntax
      FINDSTR [options] [/F:file] [/C:string] [/G:file] [string(s)] [pathname(s)]
 
Key
   string      Text to search for.
   pathname(s) The file(s) to search. 
   /C:string   Use string as a literal search string.
   /G:file     Get search string from a file (/ stands for console).
   /F:file     Get a list of pathname(s) from a file (/ stands for console).
   /d dirlist  Search a comma-delimited list of directories.
 
options may be any combination of the following switches:
/I Case-insensitive search. /S Search subfolders. /P Skip any file that contains non-printable characters /L Use search string(s) literally. /R Use search string(s) as regular expressions.(default) /B Match pattern if at the Beginning of a line. /E Match pattern if at the END of a line. /X Print lines that match exactly. /V Print only lines that do NOT contain a match.
/N Print the line number before each line that matches. /M Print only the filename if a file contains a match. /O Print character offset before each matching line. /a color_attribute Display filenames in colour (2 hex digits)

When the search string contains multiple words (separated with spaces) then FINDSTR will show show lines that contains any one word - (an OR of each word) - this behaviour is reversed if the string argument is prefixed with /C.

Regular Expressions
(Searching for patterns of text)

The FINDSTR syntax notation can use the following metacharacters which have special meaning either as an operator or delimiter.

 .         Wildcard: any character
 
 *         Repeat: zero or more occurances 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
 xyz\>     Word position: end of word

Metacharacters are most powerful when they are used together. For example, the combination of the wildcard character (.) and repeat (*) character is similar in effect to the filename wildcard (*.*)

.*         Match any string of characters

The .* expression may be useful within a larger expression, for example f.*ing will match any string beginning with F and ending with ing.

Examples:

Search for "granny" OR "Smith" in MyFile.txt.
FINDSTR "granny Smith" MyFile.txt


Search for "granny Smith" in MyFile.txt
FINDSTR /C:"granny Smith" MyFile.txt
This is effectively the same as the FIND command

To search every file in the current folder and all subfolders for the word "Smith",
regardless of upper/lower case use:

FINDSTR /s /i smith *.*


Note that /S will only search below the current directory

To find every line containing the word SMITH, preceeded by any number of spaces, and to prefix each line found with a consecutive number:

FINDSTR /b /n /c:" *smith" MyFile.txt


Finding a string only if surrounded by the standard delimiters
To find the word "computer", but not the words "supercomputer" or "computerise":

FINDSTR "\<computer\>" MyFile.txt


Now assume you want to find not only the word "computer", but also any other words that begin with the letters comp, such as "computerise" or "compete"

FINDSTR "\<comp.*" MyFile.txt

Example of a literal search

Searching a text file that contains the following

the quick brown fox
the darkbrown fox
the really *brown* fox

FINDSTR /r .*brown MyFile.txt
or
FINDSTR .*brown MyFile.txt
Will both match the word "brown" in all 3 lines

FINDSTR /L *brown* MyFile.txt
Will only match the last string

Using a script file

Multiple search criteria can be specified with a script file /G.
Multiple files to search can be specified with a source file /F.

When preparing a source or script file, place each item on a new line.

For example: to use the search criteria in Crit.txt to search the files listed in Files.txt and then store the results in the file RESULTS.txt:

FINDSTR /g:Crit.txt /f:Files.txt> Results.txt

Errorlevel

When an item is not found FINDSTR will return an errorlevel >0

Echo 12G6 |FindStr /R "[0-9]"
If %ERRORLEVEL% EQU 0 echo The string contains one or more numeric characters

Echo 12G6 |FindStr /R "[^0-9]"
If %ERRORLEVEL% EQU 0 echo The string contains one or more non numeric characters

Bugs
In early versions of FindStr /F:file a path length of more than 80 chars will be truncated.

"Twenty years from now, you will be more disappointed by the things you didn't do than by the ones you did do. So throw off the bowlines, sail away from the safe harbour. Catch the trade winds in your sails. Explore. Dream. Discover." - Mark Twain

Related:

FIND - Search for a text string in a file.
Powershell: Where-Object - Filter objects passed along the pipeline.
Equivalent bash command (Linux): grep - Search file(s) for lines that match a given pattern

Comments