Using FINDSTR to search file contents
Keywords: FINDSTR DOS Command Prompt power text string searcher within files
Tags: Tip Tutorial Windows Commandline Windows 7 Windows Server 2008 Windows XP
Entered On: 2010-07-12
Views: 141
Searching through log files for a particular string of text? How about trying to find a particular piece of source code in a large directory of files? Use FINDSTR to quickly locate the files containing the text you are looking for.
Findstr is capable of finding the exact text you are looking for in any ASCII file or files. However, sometimes you have only part of the information that you want to match, or you want to find a wider range of information. In such cases, findstr has the powerful capability to search for patterns of text using regular expressions.
Examplesfindstr "Uptime Requirement" myfile.txt
In the above example, any lines containing "Uptime Requirement" in the file "myfile.txt" would be printed to the screen.findstr /s "Uptime Requirement" *.txt
Similar to the first example, the above example would find any lines containing "Uptime Requirement" in any file with a .txt extension, in the current directory and all sub directoriesfindstr /x /c:"Uptime Requirement" *.txt
Match .txt files that contain an exact match on "Uptime Requirement". Files that contain "Uptime Requirements" or other non-exact matches will not be displayed. It is important to realize that using /x must be a line that exactly matches "Uptime Requirement". In other words, if anything else is on the same line, it's not an exact match.findstr /n /i /c:"Uptime Requirement" *
Search for any file containing "Uptime Requirement" regardless of its case and display the line where the text is found.
Command Syntax ReferenceFINDSTR [/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. |
Regular expression quick reference
Regular expressions are a notation for specifying patterns of text, as opposed to exact strings of characters. The notation uses literal characters and metacharacters. Every character that does not have special meaning in the regular expression syntax is a literal character and matches an occurrence of that character. For example, letters and numbers are literal characters. A metacharacter is a symbol with special meaning (an operator or delimiter) in the regular-expression syntax.
| . | 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 |
\| Word position: beginning of word | |
| xyz\> | Word position: end of word |
Comments On This Post
No comments on this post yet!
You must be logged in to post a comment.