Search for strings in plain text files in Windows-about the findstr command)

Source: Internet
Author: User
Tags printable characters
In Windows, searching for strings in plain text files-command findstr introduction in many cases, everyone has the feeling that the search function in Windows Resource Manager is useless. Here, I recommend a powerful findstr command. The powerful function is enough for you to search for any content in a plain text file and print out the file and row where the string is located, or even the row number. The following describes how to use findstr and some practical examples:

Findstr instructions

Findstr /?
Searches for strings in files.

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

/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.
[Drive:] [path] filename
Specifies a file or files to search.

Use spaces to separate multiple search strings unless the argument is prefixed
With/C. For example, 'findstr "hello there" x. Y' searches for "hello" or
"There" in file x. y. 'findstr/C: "hello there" x. Y' searches
"Hello there" in file x. y.

Regular expression quick reference:
. 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
\ <Xyz Word position: beginning of word
Xyz \> Word position: end of word

For full information on FINDSTR regular expressions refer to the online Command
Reference.

Findstr example:
Example 1: search for all log files containing Catalina in the logs directory of Tomcat
Findstr/R ". * Catalina .*"*
Localhost_admin_log.2009-03-18.txt: 2009-03-18 12:30:08 StandardContext [/admin] action: Tree expand/contract on Catalina: t
Ype = Service, serviceName = Catalina
Localhost_admin_log.2009-03-18.txt: 12:30:08 StandardContext [/admin] action: Found Node: Catalina: type = Service,
ServiceName = Catalina
Localhost_admin_log.2009-03-18.txt: 2009-03-18 12:30:10 StandardContext [/admin] action: Tree expand/contract on Catalina: t
Ype = Host, host = localhost

/R indicates that the given search string is treated as a regular expression. By default, the output result is in the format of File Name: row of the target string. To print the travel number, add the/N option:
Findstr/R/N ". * Catalina .*"*
Localhost_admin_log.2009-03-18.txt: 2: 12:30:08 StandardContext [/admin] action: Tree expand/contract on Catalina
: Type = Service, serviceName = Catalina
Localhost_admin_log.2009-03-18.txt: 3: 12:30:08 StandardContext [/admin] action: Found Node: Catalina: type = Servic
E, serviceName = Catalina
Localhost_admin_log.2009-03-18.txt: 5: 12:30:10 StandardContext [/admin] action: Tree expand/contract on Catalina
: Type = Host, host = localhost

If you want to save the search result to a file, use the guiding symbol>, followed by the file name:
Findstr/R/N ". * Catalina. *" *> result.txt
Use the guide character> overwrite the old content in the file. If you do not want to overwrite the old content, use the Guide character>

Example 2: In the logs directory of Tomcat
All log Content starting with 12
Findstr/N/C: "2009-03-18 12 "*
Localhost_admin_log.2009-03-18.txt: 9: 12:30:13 standardcontext [/admin] Action: Found node: Catalina: j2eetype = We
Bmodule, name =/localhost/jforum-2.1.8, j2eeapplication = none, j2eeserver = none
Localhost_admin_log.2009-03-18.txt: 10: 12:30:16 standardcontext [/admin] Action: entered treecontroltestaction: P
Erform ()
Localhost_admin_log.2009-03-18.txt: 11: 12:30:16 standardcontext [/admin] Action: Tree Param is null

Here, we use/C: "" to specify the searched string. If/C: is not used, findstr will use spaces to separate strings and then all files containing substrings separated by spaces. That is to say, by default, spaces play the role of or.

Example 3: search for all logs that do not contain standardcontext in the logs directory of Tomcat, and display the result Log File Name and row number in blue.
Findstr/n/V/A: 09 "standardcontext "*

Option/V indicates that the row that does not contain the specified string and the file are displayed in the result. Option/A: ATTR can control the color of the display result.

Other usage of findstr will not be listed here. It is worth mentioning that findstr runs very fast, even when searching dozens of megabytes of large files. From a 139m file, it takes about 18 seconds to find the string at the end of the file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.