CMD findstr string Lookup Enhanced usage Instructions _dos/bat

Source: Internet
Author: User
Tags printable characters
Look for a string in the file.
Copy Code code 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 at the beginning of a line pairing mode.
/e The pairing mode at the end of a line.
/l Use the search string by Word.
/R uses the search string as a regular expression.
/S searches the current directory and all subdirectories for matching files.
/I specifies that the search is not case-insensitive.
/x print an exact matching line.
/V prints only rows that do not contain matches.
/n Prints the number of rows before each matching row.
/M If the file contains a match, only its file name is printed.
/o Prints the character offset before each matching row.
/p ignores files that have characters that are not printable.
/off[line] does not skip files with the offline attribute set.
/A:ATTR specifies a color property that has 16 digits. Please see "color/?"
/f:file reads the file list from the specified file (/represents the console).
/c:string uses the specified string as the literal search string.
/g:file gets the search string from the specified file. (/On behalf of the console).
/d:dir find a semicolon-delimited list of directories
Strings the text you want to find.
[Drive:] [Path]filename
Specifies the file to find.

Unless the parameter has a/C prefix, use a space to separate the search string.
For example: ' FINDSTR ' Hello there "x.y" looking for "Hello" in file x.y or
"There". ' FINDSTR/C: Hello there ' x.y ' file x.y look for
"Hello there".

Quick reference to Regular expressions:
. Wildcard characters: any character
* Repeat: Previous characters or classes appear 0 or more than 0 times
^ Line position: start of line
$ row Position: End of line
[Class] character class: Any character in a character set
[^class] Complement character class: Any character that is not in the character set
[x-y] range: Any character in the specified range
\x Escape: Textual usage of the meta-character X
\<XYZ Word Position: beginning of Word
xyz\> Word Position: end of Word

For more information on common expressions of FINDSTR, see the Online command reference.
In this help message, I replace the "general expression" with "regular expression" (everything is a curse of machine translation).

Command summary:
findstr, all English find string, meaning "lookup string";

/b, all English begin, meaning "start";
/e, full English end, meaning "terminal";
/l,literally, meaning "literally", extended to "go regular expression".
/r,regular, meaning "regular expression", extended to "regular expressions".
/s,subdirectory, meaning "subdirectory";
/i,ignore, meaning "ignore", extended to "ignore the case";
/x,exactly, meaning "exactly", extended to "exact match"; (at first it was not the word, but hat was brilliant-the reason for the abbreviation for E was that it was abbreviated with the end, so it was abbreviated with the second letter X).
/v,invert, meaning "reverse, make upside down" (thanks to the words provided by DOUPIP);
/n, all English numbers, meaning "number", extended to "line number";
/m,merely, meaning "just";
/o,offset, meaning "offset";
/p,print, meaning "print";
/off[line], meaning "Offline Files";
/a,attribute, meaning "attribute";
/f,file, meaning "document";
/c,case, meaning "add up a few words", extended to "all word matching";
/g,get, meaning "obtain";
/d,directory, meaning "catalogue";
class, classes.

Thank hat for the words provided.

Thank Weichengxiehou.

Parameter detailed section 13-14 are copied from Weichengxiehou's post (since there is ready-made, how much worry), the original post address.


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

A.txt content (A.txt content in the following will be modified many times, please note!) ):
Copy Code code as follows:

Hello World
Hello Boy
Hello, good man.
goodbye!

1. Simplest application: Find the specified string in the specified text
Code:
Copy Code code as follows:

findstr "Hello" a.txt

Results:
Copy Code code as follows:

C:\users\helloworld\desktop>findstr "Hello" a.txt
Hello, good man.

Code:
Copy Code code as follows:

Findstr "Hello" a.txt

Results:
Copy Code code as follows:

C:\users\helloworld\desktop>findstr "Hello" a.txt
Hello World
Hello Boy

As you can see here,
Findstr The default is case-sensitive (like the Find command)--you won't see Hello when you look for Hello, and vice versa.
How do you make it case-insensitive?
Use the/I parameter!
For example:
Copy Code code as follows:

c:\users\helloworld\desktop>findstr/i "Hello" a.txt
Hello World
Hello Boy
Hello, good man.

2. Display the character you want to find specific in the text which line
Code: c:\users\helloworld\desktop>findstr/n/I "Hello" a.txt
To copy a code effect:
Copy Code code as follows:

1:hello World
2:hello Boy
3:hello, good man.

The result shown in the colon (:) is in English format, in the use of for extraction needs attention!
Here you can compare the/n parameter of the Find command:
Code:
Copy Code code as follows:

c:\users\helloworld\desktop>find/n "Hello" a.txt

Effect:----------A.TXT
[3]hello, Good Man.
Copy the Code colon (:) and the brackets ([]), which is the difference, be sure to write the code when you must pay attention to.
3. Find text that contains the specified character
Code:
Copy Code code as follows:

c:\users\helloworld\desktop>findstr/m/I "Hello" *.txt

Effect:
Copy Code code as follows:

1.txt
A.txt

The classes in 1.txt are as follows: unless the parameter has a/C prefix, use a space to separate the search string.
For example:
Copy Code code as follows:

' FINDSTR ' Hello there ' x.y ' Look for "hello" in file x.y or
"There". ' FINDSTR/C: Hello there ' x.y ' file x.y look for
"Hello there".
[Code]
Because the/m parameter is added, only the file name that contains the specified character is listed.
4. Find lines of text that start or end with a specified character
This feature and the previous introduction of the biggest difference is involved in the "meta character", if you do not understand what is "meta character", there is no need to worry about learning this section, it is like not understand what "water" is, it will not affect drinking water.
A.txt content:
[Code]
Good Hello
Hello World
Hello World
Hello Boy
Hello, good man.
goodbye!

How do I find rows that start with hello (ignore case)?
Two ways:
①./b parameters
Code:
Copy Code code as follows:

c:\users\helloworld\desktop>findstr/b/I "Hello" a.txt

Effect:
Copy Code code as follows:

Hello World
Hello Boy
Hello, good man.

Good Hello and Hello world, neither of these lines is shown because Hello is not at the beginning of the line.
②.^ character
The ^ here is not an escape character, but rather the "position where the matching row begins" in the regular expression.
Code:
Copy Code code as follows:

c:\users\helloworld\desktop>findstr/i "^hello" a.txt

Effect:
Copy Code code as follows:

Hello World
Hello Boy
Hello, good man.

Finished learning to find the line at which the specified character begins, learn to find the line that ends with the specified character.
How do I find a line that ends with Hello (ignoring case)?
There are also two ways:
①./e parameters
Code:
Copy Code code as follows:

c:\users\helloworld\desktop>findstr/e/I "Hello" a.txt

Results:
Copy Code code as follows:

Good Hello

Only "Good hello" is shown, because other rows have "hello", but they do not end with "Hello".
②.$ character
Code:
Copy Code code as follows:

c:\users\helloworld\desktop>findstr/i "hello$" a.txt

Result: Good Hello
By this we have learned the metacharacters of two regular expressions: ^ and $ (there are/b,/e parameters corresponding to their function respectively).
5. Find the row that exactly matches the specified character
First modify the contents of the A.txt:
Copy Code code as follows:

Hello
Hello Hello
Good Hello
Hello World
Hello World
Hello Boy
Hello, good man.
goodbye!

Knowing extrapolate's children's shoes may try the following code:
Copy Code code as follows:

c:\users\helloworld\desktop>findstr/n/I "^hello$" a.txt

The result makes you feel happy: 1:hello
In addition to this method, the FINDSTR command also provides the/x parameter to find the exact matching row.
Code:
Copy Code code as follows:

c:\users\helloworld\desktop>findstr/n/i/x "Hello" a.txt

Results:
Copy Code code as follows:

1:hello

6. What happens when you close a regular expression?
We can artificially divide the findstr into two modes, the regular expression pattern and the normal string pattern.
findstr defaults to the regular expression pattern, plus the/R parameter is also a regular expression pattern (in other words, the/R parameter is a bit redundant).
With the/l parameter, findstr is converted to "normal string mode" (in fact, find is this pattern, and only this mode).
"Normal string pattern," with the same code, see what happens?
Code:
Copy Code code as follows:

C:\users\helloworld\desktop>findstr/li "^hello" a.txt

The results show nothing.
The line beginning with Hello clearly has the following, why not show it?
Copy Code code as follows:

Hello Hello
Hello World
Hello Boy
Hello, good man.

Because, when you use the "normal string pattern", findstr does not take ^ as the meta character of a regular expression, but just as a normal character ^, which means that it does not have the function of "beginning of the line" at this time, and becomes the ordinary people with characters like H, no more "privileges".
Change the content of A.txt: ^hello
Copy Code code as follows:

Hello
Hello Hello
Good Hello
Hello World
Hello World
Hello Boy
Hello, good man.
goodbye!

Run the code again:
Copy Code code as follows:

C:\users\helloworld\desktop>findstr/nli "^hello" a.txt

Results:
Copy Code code as follows:

1:^hello

7. Find rows that do not contain the specified characters
If you compare the Find and findstr commands, they all have/v,/n,/i,/off[line parameters, and the functionality is all the same, and here's the/V parameter.
Finds rows that do not contain hello.
Code:
Copy Code code as follows:

C:\users\helloworld\desktop>findstr/vni "Hello" a.txt

Results:
Copy Code code as follows:

9:goodbye!

8. How do I find the file name containing a string in the contents of the current directory and subdirectory?
In writing this tutorial, I happened to see a group of friends ask this question, address: http://bbs.bathome.net/viewthread.php?tid=14727
Code:
Copy Code code as follows:

Findstr/ms "Professional" *.txt

Effect:
Find the text file with "professional" in the contents of the current directory and subdirectory and display only its file name.
9. Use text to make a file to find and use text to make a string to find
Use text to make a file to find
Create a new file.txt that reads as follows (this text specifies the path of the text FINDSTR to find):
Copy Code code 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 notes. txt
C:\Users\helloworld\Desktop\file.txt
C:\Users\helloworld\Desktop\MyRarHelp.txt
C:\Users\helloworld\Desktop\test.txt
C:\Users\helloworld\Desktop\ Red Mansion. txt
C:\Users\helloworld\Desktop\520\ new text 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 more commands than XP Help. txt
C:\Users\helloworld\Desktop\ Programming \wmic.txt

Code:
Copy Code code as follows:

C:\users\helloworld\desktop>findstr/f:file.txt/im "Hello"

Effect:
Copy Code code as follows:

C:\Users\helloworld\Desktop\1.txt
C:\Users\helloworld\Desktop\a.txt
C:\Users\helloworld\Desktop\CrLf Batch notes. txt
C:\Users\helloworld\Desktop\file.txt
C:\Users\helloworld\Desktop\test.txt

Use text to make a string to find
Create a new string.txt that reads as follows (this text specifies the string to findstr to find):
Copy Code code as follows:

^hello
World

A.txt
Copy Code code as follows:

^hello
Hello
Hello Hello
Good Hello
Hi, hello.
Hello World
Hello Boy
Hello, good man.
goodbye!

Code:
Copy Code code as follows:

C:\users\helloworld\desktop>findstr/ig:string.txt A.txt

Effect:
Copy Code code as follows:

Hello
Hello Hello
Hello World
Hello Boy
Hello, good man.

Ignored rows
Copy Code code as follows:

^hello
Good Hello
Hi, hello.
goodbye!

It can be seen from the neglected "^hello" that, without the/l parameter, the search string specified with/g, if it contains "metacharacters", is used as a regular expression instead of as a normal expression.
10. Search for a perfectly matched sentence
In fact, there is a good example of Findstr's help:
For example: ' FINDSTR ' Hello there "x.y" looking for "Hello" in file x.y or
"There". ' FINDSTR/C: Hello there ' x.y ' file x.y look for
"Hello there".
You can use this example to do a test.
Copy Code code as follows:

A.txthello there
Hellothere
Hello
There

Code:
Copy Code code as follows:

C:\users\helloworld\desktop>findstr/ic: "Hello there" a.txt

Results:
Copy Code code as follows:

Hello there

This is the exact match of the sentence.
11. Search for a perfectly matched word.
This also involves two meta character:\<,\>.
Let's look at an example.
A.txt
Copy Code code as follows:

Far there
Farthere
There
Far
Farm
Farmer

Code:
Copy Code code as follows:

C:\users\helloworld\desktop>findstr "Far" a.txt

Results:
Copy Code code as follows:

Far there
Farthere
Far
Farm
Farmer

My intention is to find a line containing the word "far", but Farthere, farm, farmer are showing up, which is not the result I want.
How do you write if you only want to show the line containing the word "far"?
Code:
Copy Code code as follows:

C:\users\helloworld\desktop>findstr "\<far\>" a.txt

Results:
Copy Code code as follows:

Far there
Far

12. Specify the directory to find
/d parameters I've always classified it with/F,/g, but in fact they are very different,/F,/g is a text file to find the file, string, and/d is the direct writing directory name to the command.
Code:
Copy Code code as follows:

c:\users\helloworld\desktop>findstr/imd:520; programming; ". *" "*.txt"

Results:
Copy Code code as follows:

520:
Hello.txt

Programming:
Help.txt
Copy Code code as follows:

Win7 Help has more commands than XP Help. txt
Wmic.txt

Find all txt files containing any character in the 520, programming directory.
13. Statistics of characters
/o: Prints the character offset before each line and prints the beginning of the start of the line before each row is found, that is, how many characters, such as the following in Test.txt:
Copy Code code as follows:

Aaaaaaaaaa
Aaaaaaaaaa
Aaaaaaaaaa
Aaaaaaaaaa
Aaaaaaaaaa
Aaaaaaaaaa

Execute command: findstr/o. * Test.txt
Copy code:: the. * In the previous line is the contents of the regular expression, representing any row, including a blank line
The results are as follows:
Copy Code code as follows:

0:aaaaaaaaaa
12:aaaaaaaaaa
24:aaaaaaaaaa
36:aaaaaaaaaa
48:aaaaaaaaaa

Note that the return line feed at the end of each line is two characters.
14. Display the file name in the specified color
/A: Specify the color attributes for the file name section of the search results when the search file name contains wildcard * or?, and the specific color value see 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 = Light Yellow
7 = White F = Bright White
Often used in color display, for example, want to color display "batch processing House" How to do, if the current color set to 27 (background green, font white), in blue display "batch of home" do? :: The next line of backspace can be in the cmd edit mode press ctrl+p after pressing the backspace button to obtain > "Batch processing House" Set/p=<nul
Copy Code code as follows:

> "Batch processing House" Set/p=<nul
findstr/a:21. * "Batch Home *"
Pause

The backspace in the code is to allow the displayed content to be only a "batch house", and if there are other content, there is a colon and other content after the "batch house" displayed in color, and backspace just removes the colon. Note that wildcard characters in your code are required.
Meta characters in 15.FINDSTR
16. Content not explained:/p,/off[line]
These two commands do not understand what is meant, because do not know what is "not printable characters", "with the offline property set of files," Look at people to answer.

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.