Batch processing Getting Started manual for batches Common DOS command posts (find findstr) _dos/bat

Source: Internet
Author: User

1.4 Learning Find findstr command

Learning points:
1. String Lookup: Find
2. String Lookup Enhancements: findstr

A. String lookup: Find

Searches for a string in a file.
Find [/v] [/C] [/n] [i] [/off[line]] "string" [[drive:][path]filename[...]]

/V Displays all rows that do not contain the specified string.
/C displays only the number of rows containing the string.
/n Displays line numbers.
/I ignores case when searching for strings.
/off[line] Do not skip files that have an offline attribute set.
"string" specifies the text string to search for.
[Drive:] [path]filename Specifies the file to search for.


Basic format: Find "string to find" the file to look for (the full path is not required in the current directory)

Example 1
Find "ABC" D:\abc.txt
Finds the line of the string ABC in Abc.txt.


/I ignores case when searching for strings.

Example 2
find/i "ABC" D:\abc.txt
The parameter/I represents "Ignore" (ignored), which is to ignore case. The/I parameter can not distinguish the case of the string "ABC" to be found.


/n Displays line numbers.

Example 3
find/n "ABC" D:\abc.txt
The parameter/n represents the English word "number". With the/n parameter we can find the line number where the string "ABC" is located.


/C displays only the number of rows containing the string.

Example 4
FIND/C "ABC" D:\abc.txt
Parameter/c is the abbreviation for the English word "count" (count). With the/C parameter we can count the number of rows that contain the "ABC" string.


/V Displays all rows that do not contain the specified string.

Example 5
find/v "ABC" D:\abc.txt
This parameter is used to find the row in the file that does not contain the specified string. Here represents the lookup of a row that does not contain an "ABC" string.

Note: The word string to look up in the Find command is enclosed in double quotes "".

Two. String Lookup enhancements: findstr

Look for a string in the file.
FINDSTR [/b] [/e] [/l] [/R] [/S] [/i] [/x] [/v] [/n] [/M] [///O] [/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 generic expression.
/S Search in the current directory and all subdirectories
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 ' looks for "hello there" in the file x.y.


1. Basic format: FINDSTR "strings" [Drive:][path]filename

Strings is what you are looking for.
[Rive:] [path]filename Specifies the file to find, the path can be default, and the current directory by default.

Example 1
Findstr "ICQ" 123.txt
Look for rows in 123.txt that contain three strings with "ICQ".


/I specifies that the search is not case-insensitive.

Example 2
findstr/i "MSN" 123.txt
Locate the three-character line containing "MSN" in 123.txt and is case-insensitive.


★/R uses the search string as a regular expression. Parameter/R emphasizes the use of regular expression rules to interpret strings. R-right right is positive. We
All say the right hand is forehand, so extended to the right is positive, the left is reversed.

Example 3
FINDSTR/R "ICQ MSN" 123.txt
Find lines in 123.txt that contain "ICQ" or "MSN", and separate the multiple strings found with spaces between them.


/S searches in the current directory and all subdirectories.

Example 4
FINDSTR/S/I "MSN" *.txt
Searches the txt file in the current directory and all subdirectories for the string "MSN" (case-insensitive).


/c:string uses the specified string as the literal search string.

Example 5
FINDSTR/C: "ICQ MSN" 123.txt
Look for lines in 123.txt that contain several characters of "ICQ MSN". Note that the "ICQ MSN" here is a whole.
This parameter is used to find strings that contain spaces.


★ In the use of findstr "I you He" test.txt, and can not find content, but, plus switch/I or r after the correct
, it may be a bug when looking up multiple pure Chinese strings, and there is no problem with a single pure Chinese string.

Rule of use for regular expressions in the 2.FINDSTR command

Quick reference to General expressions:
. Wildcard characters: any character
* Repeat: Previous characters or categories appear 0 or more than 0 times
^ Line position: start of line
$ row Position: End of line
[Class] character category: Any character in the character set
[^class] Complement character category: 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


Wildcard and repeat rules, that is,. and
Wildcard characters, that is, a period that represents any one character, and can only be one, including letters, numbers, half-width symbols, and spaces.
The repeat character, the model * represents the number of occurrences of the preceding letter (the number of occurrences from 0 to many times, and 0 indicates no).

Findstr. 123.txt or findstr "." 123.txt
Find any character in file 123.txt, excluding blank lines.

Example 6
findstr. * 2.txt or findstr ". *" 2.txt
Look for any character in file 123.txt, including blank lines.

Example 7
findstr ac* 123.txt
Find a "a" string in file 123.txt, and a word rows that appears 0 or more times after a.
Such as:
A
Ac
Acc
Addc
And so on are matched.

Example 8
findstr ak5* 123.txt
Find an "AK" string in file 123.txt, and a character rows 0 or more times after AK. Such as:
Ak
Ak5
akbbb
ak125
ak555
And so on are matched.


Beginning, line-tailed rules, i.e. ^ and $

Example 9
Findstr "^step" 123.txt
Finds a line in file 123.txt that has the beginning of the step string.
Such as:
Stepkdka
Step 456
Both of these lines match.

Example 10
Findstr "step$" 123.txt
Finds the line at the end of a line in file 123.txt as the step string.
Such as:
123 Dstep
123step
The two lines also match.

Example 11
Findstr "^step$" 123.txt
Find the line at the beginning of the start of the file 123.txt, and the line at the end of step, that is, step alone.


Character set rules, that is, [class]
① represents a match that contains any character in a set.
② the elements in this character set can be alphabetic and numeric and generally half-width characters, such as:} {,.] [etc, but double quotes "are not recognized.) Can not be Chinese characters, Chinese characters
is not interpreted correctly (Chinese characters are not ASCII code).
If you insert wildcards and repeating symbols within the character set, that is, "[. *]" will. and * as ordinary characters, without the meaning of the wildcard and repetition.

Example 12
Findstr "[0-9]" 123.txt
Look for a row in file 123.txt for any one of the number 0-9.
Such as:
4kkb
1 Lkka cc
Both of these lines match.

Example 13
Findstr "[A-za-z]" 123.txt
Look for the file 123.txt to include any letter lines.

Example 14
Findstr "[Abcezy]" 2.txt
Locate the line in file 123.txt that contains any of the letters a B c e Z y.

Example 15
Findstr "[A-fl-z]" 2.txt
In file 123.txt, look for the lines of any letter from lowercase A to f or L to Z, but do not include the letters G H I J K.

Example 16
Findstr "M[abc][123]y" 2.txt
Find in file 123.txt to match ma1y, mb1y, mc1y; MA2Y, mb2y, mc2y; Ma3y, mb3y, Mc3y's line.


Subtraction rule, that is, [^class]

Example 17
Findstr "[^0-9]" 123.txt
If a pure number of lines filtered out, such as 2323423423 of such a string is filtered, 345hh888 such a form can not filter.
Note that a pure number of lines can not have spaces, regardless of the beginning of the line at the end or the line can not have spaces, or filter failed!

Example 18
Findstr "[^a-z]" 123.txt
If the line of the pure letter is filtered out, such as SDLFJLKJLKSJDKLFJLSKDF characters will be filtered, if it is sdfksjdkf99999 such a shape
Type is not filtered.
Note that a plain letter line cannot have spaces, whether or not there are no spaces in the line at the beginning or in the row, otherwise filtering fails!

Example 19
Findstr "[^add]" 123.txt
Filters only the rows that contain a plain letter string consisting of a D D three letters.
Such as:
A
Ad
Ddaadd
Dd
These rows will be filtered.
Note that a line that contains only a plain letter string consisting of a D D three letters cannot have spaces, regardless of whether there are spaces at the beginning or end of the line or in the row, otherwise
Filter failed!

Example 20
Findstr "[^echo]" 123.txt
Filter lines that contain only plain letter strings consisting of E c h o four letters.
Such as:
E
C
Ec
Cho
Chooo
These rows will be filtered.


Word prefix suffix positioning rules, i.e. \<XYZ and xyz\>

The XYZ can be an English word or number, but it does not apply to Chinese characters. The symbol \ is interpreted as an escape character, which dissolves the redirection command meaning of less than and greater than numbers.
The rule is to match a single English word.

Example 21
Findstr "\<echo" 123.txt
All rows that contain strings prefixed by echo are matched.
Such as:
ECHO:KKK a add
Jjkk Echo
Both of these lines match.
(thinking: Why: Echo also matches?) )

Example 22
Findstr "Echo\>" 123.txt
All rows that contain strings with the echo suffix are matched.
Such as:
QQ Bbecho this line also matches.
(thinking: Why KKKK Echo: also match?) )

Example 23
Findstr "\<end\>123.txt
Here is the exact search for the word. Find the line for the word end,
Attention:
Ended
Cdkend
Bcd-end-jjkk
None of these words match.
(think: Why does end echo and end match?) Because the \<xyz\> format is looking for a single English word. )


Escape character \
Converts special characters (meta characters) in an expression to normal characters. Common wording:
\.
\*
\\
\[
\]??
\-

Example 24
Findstr "\.ABC" 123.txt
Find the line in file 123.txt that matches the ". ABC" string, here \. is to put. To escape.

Example 25
Findstr "1\\" 123.txt or findstr "1\\\\" 123.txt
Find the line in file 123.txt that can match the "1\" string, where \ \ is escaping.

★ To find the string contains \ Time, you can use \ \ to escape, or turn \ into \\\\. Search for characters if the target string has content behind it
String \ In addition to becoming \ (its own escape requirements), you can also add a character after it, such as \ \.

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.