March 31, 2013

Findstr - a windows equivalent to Unix grep

Findstr - a Windows string parser similar to grep 

Findstr 

findstr [/b] [/e] [/l | /r] [/s] [/i] [/x] [/v] [/n] [/m] [/o] [/p] [/f:<File>] [/c:<String>] [/g:<File>] [/d:<DirList>] [/a:<ColorAttribute>] [/off[line]] <Strings> [<Drive>:][<Path>]<FileName>[ ...] 

Parameters
/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.


Regular expression metacharacters that can be used with findstr:


.
Wildcard: any character
*
Repeat: zero or more occurrences of the previous character or class
^
Line position: beginning of the line
$
Line position: end of the line
[class]
Character class: any one character in a set
[^class]
Inverse class: any one character not in a set
[x-y]
Range: any characters within the specified range
\x
Escape: literal use of a metacharacter x
\<string
Word position: beginning of the word
string\>
Word position: end of the word



examples

To search for "smith" in the file named myfile.txt 
findstr smith myfile.txt

To search for "John Smith" in the file myfile.txt
findstr /c: "John Smith" myfile.txt