Searches

Advanced Search

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.

Examples

findstr "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 directories


findstr /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 Reference

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[ ...]]

/BMatches pattern if at the beginning of a line.
/EMatches pattern if at the end of a line.
/LUses search strings literally.
/RUses search strings as regular expressions.
/SSearches for matching files in the current directory and all subdirectories.
/ISpecifies that the search is not to be case-sensitive.
/XPrints lines that match exactly.
/VPrints only lines that do not contain a match.
/NPrints the line number before each line that matches.
/MPrints only the filename if a file contains a match.
/OPrints character offset before each matching line.
/PSkip files with non-printable characters.
/OFF[LINE]Do not skip files with offline attribute set.
/A:attrSpecifies color attribute with two hex digits. See "color /?"
/F:fileReads file list from the specified file(/ stands for console).
/C:stringUses specified string as a literal search string.
/G:fileGets search strings from the specified file(/ stands for console).
/D:dirSearch a semicolon delimited list of directories
stringsText to be searched for.
[drive:][path]filenameSpecifies 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
\xEscape: 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.

 

Did You Know?

The word 'cereal' derives from the name of the Roman goddess Ceres, protector of crops.