Cmd findstr string SEARCH enhancement instructions

Source: Internet
Author: User

Search for strings in the file. Copy codeThe Code is as follows: 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 starts the pairing mode on a row.
/E matches the end of a row.
/L use search strings by words.
/R uses the search string as a regular expression.
/S searches for matched files in the current directory and all subdirectories.
/I indicates that the search is case-insensitive.
/X print the exact matched rows.
/V only prints the rows that do not contain the matching data.
/N prints the number of rows before each row.
/M if the file contains a match, only the file name is printed.
/O prints the character offset before each matching row.
/P ignores files with printable characters.
/OFF [LINE] does not skip files with offline attribute sets.
/A: attr specifies the color attribute of A hexadecimal number. See "color /? "
/F: the list of files read from a specified file (/indicates the console ).
/C: string uses the specified string as the text search string.
/G: file obtains the search string from the specified file. (/Stands for the console ).
/D: dir: Find the list of directories separated by semicolons
Strings text to be searched.
[Drive:] [path] filename
Specifies the file to be searched.

Unless the parameter has a/C prefix, use spaces to separate the search string.
For example, 'findstr "hello there" x. Y' searches for "hello" or
"There ". 'Findstr/C: "hello there" x. Y' file x. y
"Hello there ".

Quick Reference for regular expressions:
. Wildcard: any character
* Repetition: Previous characters or classes that appear zero or more times
^ Row position: Start of a row
$ Row position: end of the row
[Class] character class: any character in the character set
[^ Class] character population class: any character that is not in the character set
[X-y] range: any character in the specified range
\ X Escape: Text usage of metacharacters x
\ <Xyz position: Start of a word
Xyz \> word position: End of a word

For more information about the common expressions of FINDSTR, see online command reference.
In this help message, I replaced all "general expressions" with "Regular Expressions" (everything is a fault caused by machine translation ).

Command summary:
Findstr: find string in English, meaning "search string ";

/B, all begin English, meaning "start ";
/E, all English end, meaning "end ";
/L, literally, stands for "literally"; extended to "de-Regular Expression ".
/R, regular, which indicates "regular" and extended to "regular Expression ".
/S, subdirectory, meaning "subdirectory ";
/I, ignore, indicating "ignore"; extended to "ignore case ";
/X, exactly, meaning "exactly"; extended to "exact match"; (at the beginning, it means not the word, but HAT is really brilliant. The reason is e, because the abbreviation of end exists in the beginning, the abbreviation is the second letter x ).
/V, invert, meaning "reverse and reverse" (thanks to the word doupip );
/N, full English number, indicating "number"; extended to "line number ";
/M, merely, meaning "only ";
/O, offset, indicating "offset ";
/P, print, meaning "print ";
/Off [line], meaning "offline file ";
/A, attribute, meaning "attribute ";
/F, file, meaning "file ";
/C, case, meaning "add up a few words"; extended to "match all words ";
/G, get, meaning "get ";
/D, directory, meaning "directory ";
Class.

Thanks to the words provided by HAT.

Thanks to weichengxiehou.

The Parameter Details section 13-14 is copied from weichengxiehou's post (since there is a ready-made, worry-free), the original post address.

Parameters:
Learning findstr requires a lot of practical experience, so you need to create some txt text for testing.

The content of a.txt will be modified multiple times later. Please note !) :Copy codeThe Code is as follows: Hello World
Hello Boy
Hello, good man.
Goodbye!

1. Simple Application: Find the specified string in the specified text
Code:Copy codeThe Code is as follows: findstr "hello" a.txt

Result:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr "hello" a.txt
Hello, good man.

Code:Copy codeThe Code is as follows: findstr "Hello" a.txt

Result:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr "Hello" a.txt
Hello World
Hello Boy

We can see that,
Findstr is case-sensitive by default (the same as the find command)-"hello" is not displayed if you are looking for "Hello", and vice versa.
How can we make it case insensitive?
Use/I parameters!
For example:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/I "Hello" a.txt
Hello World
Hello Boy
Hello, good man.

2. display the specific line of characters to be searched for in the text
Code: C: \ Users \ helloworld \ Desktop> findstr/n/I "hello" a.txt
Copy the Code:Copy codeThe Code is as follows: 1: Hello World
2: Hello Boy
3: hello, good man.

In the displayed result, the colon (:) is in English format. When using for extraction, pay attention to it!
Here we can compare the/n parameters of the find command:
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> find/n "hello" a.txt

Effect: ---------- A. TXT
[3] hello, good man.
Copy the colon (:) and brackets ([]) of the Code. This is the difference. Be sure to write the code.
3. Search for text that contains the specified characters
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/m/I "hello" *. txt

Effect:Copy codeThe Code is as follows: 1.txt
A.txt

The class content in 1.txt is as follows: unless the parameter has a/C prefix, use spaces to separate the search string.
For example:Copy codeThe Code is as follows: 'findstr "hello there" x. Y' searches for "hello" or
"There ". 'Findstr/C: "hello there" x. Y' file x. y
"Hello there ".
[Code]
Because the/m parameter is added, only names containing the specified characters are listed.
4. Search for text lines starting or ending with a specified character
The biggest difference between this function and the previous introduction is that it involves "metacharacters". If you don't understand what "metacharacters" are, you don't have to worry about learning this section, this does not seem to understand what "water" is, nor will it affect drinking water.
A.txt content:
[Code]
Good hello
Hello world
Hello World
Hello Boy
Hello, good man.
Goodbye!

How do I find rows starting with hello (Case Insensitive?
Two methods:
①./B Parameters
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/B/I "hello" a.txt

Effect:Copy codeThe Code is as follows: Hello World
Hello Boy
Hello, good man.

Good hello and hello world are not displayed Because hello is not at the beginning of the line.
②. ^ Character
The ^ here is not an escape character, but a regular expression that matches the starting position of a row ".
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/I "^ hello" a.txt

Effect:Copy codeThe Code is as follows: Hello World
Hello Boy
Hello, good man.

After learning to find the rows starting with a specified character, we will learn to find the rows ending with a specified character.
How do I find rows ending with hello (case-insensitive?
There are two methods:
①./E parameters
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/e/I "hello" a.txt

Result:Copy codeThe Code is as follows: good hello

Only "good hello" is displayed, because although other rows have "hello", they do not end with "hello.
②. $ Operator
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/I "hello $" a.txt

Result: good hello
At this point, we have learned the metacharacters of two regular expressions: ^ and $ (the/B and/e parameters correspond to their functions respectively ).
5. Search for rows that exactly match the specified character
First, modify the.txt content:Copy codeThe Code is as follows: hello
Hello
Good hello
Hello world
Hello World
Hello Boy
Hello, good man.
Goodbye!

Try the following code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/n/I "^ hello $" a.txt

The results make you very happy: 1: hello
In fact, in addition to this method, the findstr command also provides the/x parameter for searching for completely matched rows.
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/n/I/x "hello" a.txt

Result:Copy codeThe Code is as follows: 1: hello

6. What if I disable a regular expression?
We can artificially divide findstr into two modes: "regular expression mode" and "normal string mode ".
The default findstr format is "regular expression mode", and the/r parameter is also "regular expression mode" (in other words, the/r parameter is a bit redundant ).
After the/l parameter is added, findstr is converted to "normal string mode" (in fact, find is this mode, and only this mode ).
In "normal string mode", the same code is used to see how the result works?
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/li "^ hello" a.txt

The results are not displayed.
The following lines starting with hello are clearly displayed. Why?Copy codeThe Code is as follows: hello
Hello World
Hello Boy
Hello, good man.

Because, when you use the "normal string mode", findstr does not regard ^ As a metacharacter of a regular expression, but just treats it as a normal character ^, that is to say, it does not have the function of "representing the beginning of a line" at this time. It becomes a common citizen with the same characters as h and has no "Privilege ".
Change a.txt content: ^ helloCopy codeThe Code is as follows: hello
Hello
Good hello
Hello world
Hello World
Hello Boy
Hello, good man.
Goodbye!

Run the code again:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/nli "^ hello" a.txt

Result:Copy codeThe Code is as follows: 1: ^ hello

7. Search for rows that do not contain the specified characters
If you compare the find and findstr commands, you will find that they all have/v,/n,/I,/off [line] parameters, and the functions are the same, this is the/v parameter.
Search for rows that do not contain hello.
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/vni "hello" a.txt

Result:Copy codeThe Code is as follows: 9: goodbye!

8. How do I find the file name containing a string in the file content in the current directory and subdirectory?
At the time of writing this tutorial, I accidentally saw a batch of friends asked this question, the problem address: http://bbs.bathome.net/viewthread.php? Tid = 14727
Code:Copy codeCode: findstr/MS "professional" *. txt

Effect:
Find the text file containing "professional" in the current directory and subdirectory, and display only the file name.
9. Use text to specify the file to be searched And use text to specify the string to be searched
Use text to create the file to be searched
Create a new file.txt with the following content (This text specifies the path of the findstr text to be searched ):Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop \ 1.txt
C: \ Users \ helloworld \ Desktop \ a.txt
C: \ Users \ helloworld \ Desktop \ clip.txt
C: \ Users \ helloworld \ Desktop \ CrLf batch processing example .txt
C: \ Users \ helloworld \ Desktop \ file.txt
C: \ Users \ helloworld \ Desktop \ MyRarHelp.txt
C: \ Users \ helloworld \ Desktop \ test.txt
C: \ Users \ helloworld \ Desktop \ cmd.txt
C: \ Users \ helloworld \ Desktop \ 520 \ new release document .txt
C: \ Users \ helloworld \ Desktop \ 520 \ 12 \ hello _ world.txt
C: \ Users \ helloworld \ Desktop \ programming \ help.txt
C: \ Users \ helloworld \ Desktop \ programming \ win7 help is more imperative than xp help.txt
C: \ Users \ helloworld \ Desktop \ programming \ wmic.txt

Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/f: file.txt/im "hello"

Effect:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop \ 1.txt
C: \ Users \ helloworld \ Desktop \ a.txt
C: \ Users \ helloworld \ Desktop \ CrLf batch processing example .txt
C: \ Users \ helloworld \ Desktop \ file.txt
C: \ Users \ helloworld \ Desktop \ test.txt

Use text to specify the string to be searched
Create a new string.txt with the following content (This text specifies the findstr string to be searched ):Copy codeThe Code is as follows: ^ hello
World

A.txtCopy codeThe Code is as follows: ^ hello
Hello
Hello
Good hello
Hello
Hello World
Hello Boy
Hello, good man.
Goodbye!

Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/ig: string.txt a.txt

Effect:Copy codeThe Code is as follows: hello
Hello
Hello World
Hello Boy
Hello, good man.

Ignored rowsCopy codeThe Code is as follows: ^ hello
Good hello
Hello
Goodbye!

We can see from the ignored "^ hello" that, without the/l parameter, if the search string specified by/g contains "metacharacters ", it is used as a regular expression instead of a normal expression.
10. Search for a completely matched sentence
In fact, there is a good example in the help provided by findstr:
For example, 'findstr "hello there" x. Y' searches for "hello" or
"There ". 'Findstr/C: "hello there" x. Y' file x. y
"Hello there ".
You can use this example to perform a test.Copy codeThe Code is as follows: a.txt hello there
Hellothere
Hello
There

Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/ic: "hello there" a.txt

Result:Copy codeThe Code is as follows: hello there

This is the exact match of the sentence.
11. Search for a completely matched word.
Two metacharacters are also involved: \ <, \>.
First, let's explain an example.
A.txtCopy codeThe Code is as follows: far there
Farthere
There
Far
Farm
Farmer

Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr "far" a.txt

Result:Copy codeThe Code is as follows: far there
Farthere
Far
Farm
Farmer

My intention is to search for rows containing the word "far", but farthere, farm, and farmer show that this is not the result I want.
If you only want to display rows containing the word "far", how can this problem be solved?
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr "\ <far \>" a.txt

Result:Copy codeThe Code is as follows: far there
Far

12. Specify the directory to be searched
I have always classified the/d parameter as/f and/g, but in fact they are completely different./f and/g are the files and strings to be searched using text files, and/d directly writes the directory name to the command.
Code:Copy codeThe Code is as follows: C: \ Users \ helloworld \ Desktop> findstr/imd: 520; programming; ". *" "*. txt"

Result:Copy codeCode 520:
Hello.txt

Programming:
Help.txtCopy codeThe Code is as follows: win7 help has more commands than xp help.txt
Wmic.txt

Find all txt files containing arbitrary characters in the 520 and programming directories.
13. Count characters
/O: contains the following content:Copy codeThe Code is as follows: aaaaaaaaaa
Aaaaaaaaaa
Aaaaaaaaaa
Aaaaaaaaaa
Aaaaaaaaaa
Aaaaaaaaaa

Run the command: findstr/o. * test.txt
Copy Code: The. * In the previous row is the content of the regular expression, indicating any row, including empty rows.
The result is as follows:Copy codeThe Code is as follows: 0: aaaaaaaaaa
12: aaaaaaaaaa
24: aaaaaaaaaa
36: aaaaaaaaaa
48: aaaaaaaaaa

Note that the line breaks at the end of each line are two characters.
14. display the file name in a specified color
/A: When the searched file name contains wildcards * or? Specify the color attribute for the file name of the search result. For specific color values, see the color help:
0 = black 8 = gray
1 = blue 9 = light blue
2 = Green A = light green
3 = light green B = light green
4 = red C = light red
5 = purple D = lavender
6 = yellow E = pale yellow
7 = white F = bright white
It is often used for color display. For example, if you want to display the batch processing home in color, if the current color is set to 27 (the background is green and the font is white ), what should I do if "batch processing home" is displayed in blue? : Return character of the next row

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.