JSI Tip 0321 - String search from the command line or in a batch file.
November 19, 1997
Windows NT supports findstr which searches files for literal strings or regular expressions.
findstr [/b] [/e] [/l] [/c:string] [/r] [/s] [/i] [/x] [/v] [/n] [/m] [/o] [/g:file] [/f:file] strings files
Parameters | M e a n i n g |
---|---|
/b | Match pattern at beginning of the line. |
/e | Match pattern at end of the line. |
/l | Search literally. |
/c | Use text as a literal. /c:"string" |
/r | Use text as a regular expression (default). |
/s | Search sub-directories. |
/i | Case insensitive. |
/x | Selects lines that are an exact match. |
/v | Selects lines that do not match. |
/n | Displays the line number before the matched line. |
/m | Displays only the matching file names. |
/o | Displays the offset of the match before the matched line. |
/g | Gets the search string from the specified file. /g:argument.txt |
/f | Gets the file list from the specified file. /f:filelist.txt |
strings | The search string. |
files | Files to be searched. |
Use spaces to separate multiple search strings unless the argument is prefixed with /c. Example:
findstr "Windows NT" document.txt searches for "Windows" or "NT" in file document.txt.
findstr /c:"Windows NT" document.txt searches for "Windows NT" in file document.txt.
Regular expressions are a notation for specifying patterns, as opposed to exact strings. Characters that do not have special meaning are considered literal character and match an occurance of that character. Letters and numbers are literal characters. A metacharacter is a symbol with special meaning (an operator or delimiter) in the regular-expression syntax:
. | Wildcard. |
---|---|
* | Repeating character, Match on zero or more occurances of the previous character or class. |
^ | Line position - beginning of line. |
$ | Line position - end of line. |
[class] | match any character in the set. |
[^class] | match if any character is not in the set. |
a-z | Range: match if any characters are in the range. |
a | Literal use of metacharacter a. |
Word position - beginning of word. | |
abc> | Word position - end of word. |
Examples:
To find all occurance of a string begining with W and ending with ws: findstr "W.*ws" document.txt
To find all occurances of Windows: findstr Windows document.txt
To find all occurances of Windows in c:WinNT and its' sub-directories, ignoring case: findstr /s /i Windows c:WinNT*.*
To find several strings in a list of files: findstr /g:argument.txt /f:files.txt
To list every file on the C: drive that contains the word JSI: findstr /s /m "" C:*.*
To find every file in the current directory that contains a word that starts with Win: findstr /m "
About the Author
You May Also Like