Batch-processing regular expression (findstr) collation _dos/bat

Source: Internet
Author: User
Tags character classes
Grammar
findstr [/b] [/e] [/l] [/R] [/s] [/i] [/x] [/v] [/n] [m] [o] [P] [/s] [/s] [/offline] [/g:file] [/f:file] [/c:string] [/d:dirlist ] [/a:colorattribute] [Strings] [[Drive:][path] FileName [...]]

Parameters

/b
Matches the pattern if it is at the beginning of the line.

/e
Matches the pattern if it is at the end of the line.

/L
Search the string verbatim.

/R
Use the search string as a regular expression. Findstr interprets all metacharacters as regular expressions, unless L is used.

/s
Searches the current directory and all subdirectories for matching files.

/I
Specifies that the search is case-insensitive.

/x
Prints an exact matching row.

/V
Print only rows that do not contain matches.

/n
The line number is printed before each matching row.

/m
If the file contains a match, only the file name is printed.

/O
Prints the lookup offset before each matching row.

/P
Skips files that contain nonprinting characters.

/offline
Use offline property settings to work with files.

/f:file
Reads a list of files from the specified file.

/c:string
Searches for a string using the specified text as a literal.

/g:file
Gets the search string from the specified file.

/d:dirlist
Searches for a comma-separated list of directories.

/a:colorattribute
Specifies a color property using two hexadecimal digits.

Strings
Specifies the text to search for in FileName.

[Drive:] [Path] FileName [...]

Specifies the file to search for.

/?
Displays help at the command prompt.

Comments

• Use Strings and [Drive:][path] FileName [...]
In the command string, all findstr command-line options must precede Strings and [Drive:][path] FileName [...].

• Use regular Expressions in findstr
Findstr can find exactly the text you want to find in any ASCII file or file. However, there are times when the information to be matched is only part of or to look for a broader range of information. In this case, FINDSTR has a powerful ability to search for various texts using regular expressions.

A regular expression is a symbol used to specify the type of text, as opposed to an exact string. tags use literal character and metacharacters characters. Each character that has no special meaning in the regular expression syntax is a literal character that matches the character that appears. For example, letters and numbers are literal symbols. Metacharacters are symbols that have special meaning (operators or delimiters) in the regular expression syntax.

The following table lists the meta characters that Findstr accepts.

Character value

.
Wildcard characters: any character

*
Repetition: A character or class that appears 0 or more times before

^
Position in row: beginning of line

$
Position in line: End of line

[Class]
Character classes: Any one character in a set

[^class]
Reverse class: Any one character not in a set

[X-y]
Scope: Any character within the specified range

\x
Escaping: literal use of meta character X

\ position of Word: first word

Xyz\>
Position of Word: end of Word

The special characters used to combine regular expression syntax are powerful. For example, the combination of the following wildcard characters (.) and the repeat character (*) can match any string:

.*

Use the following expression as part of a larger expression that matches any string that begins with "B" and ends with "ing":

B.*ing

Example
Use spaces to separate multiple search strings, unless the parameters are prefixed with/C. To search for "Hello" or "there" in file x.y, type:

findstr "Hello there" x.y

To search for "Hello there" in file x.y, type:

FINDSTR/C: "Hello there" x.y

To find all the words "Windows" (first letter W) that appear in the file Proposal.txt, type:

findstr Windows Proposal.txt

To search for each file in the current directory and all subdirectories that contain the word Windows (regardless of the case of letters), type:

FINDSTR/S/I Windows *.*

To find all the lines that contain the word "for" (preceded by any space, such as a loop in a computer program), and include the line number that appears each time, type:

findstr/b/n/c: "*for" *.bas

If you want to search for several different items in the same group file, create a text file that contains each search criterion on a new line. You can also list the exact files you want to search in a text file. To use the search criteria in file Finddata.txt to search for files listed in Filelist.txt, and then save the results to a file results.out, type:

Findstr/g:finddata.txt/f:filelist.txt > Results.out

Suppose you want to find each file that contains the word "computer" in the current directory and all subdirectories, regardless of the case of the letter. To list all the files that contain the word "computer", type:

findstr/s/i/m "\" *.*

Now suppose you need to find not only the word "computer", but also all other words that start with the letter "comp", such as "compliment" and "compete". Please type:

findstr/s/i/m "\

http://www.microsoft.com/technet/prodtechnol/windowsserver2003/zh-chs/library/ServerHelp/ 2b01d7f5-ab5a-407f-b5ec-f46248289db9.mspx?mfr=true

Instance:

1.findstr. 2.txt or findstr "." 2.txt
Find any character from file 2.txt, excluding null characters or blank lines
====================

2.findstr. * 2.txt or findstr ". *" 2.txt
Find any character from file 2.txt including blank lines and null characters
====================

3.findstr "[0-9]" 2.txt
Find a string or row that includes the number 0-9 from file 2.txt
====================

4.findstr "[A-za-z]" 2.txt
Find a string or row that includes any character from file 2.txt
====================

5.findstr "[Abcezy]" 2.txt
Find a string or line that includes a b c e z y letter from File 2.txt
====================

6.findstr "[A-fl-z]" 2.txt
Finds the lowercase character a-f l-z string from file 2.txt, but does not contain the letters G H I J K.
====================

7.findstr "M[abc][hig]y" 2.txt
From the file 2.txt can match Mahy, Mbiy, Mahy, etc. .....
====================

8. ^ and $ application of symbols
^ Represents the beginning of the line, and "^step" matches only the first word in "Step Hello World"
$ indicates line end, "step$" matches only the last word in "Hello World"
====================

9.finstr "[^0-9]" 2.txt
If it is a string of pure numbers or a line filtered out, such as 2323423423 string, if it is 345hh888 such a form will not be.
====================

10.findstr "[^a-z]" 2.txt
Ditto, if the string or line of pure letters filtered out, such as sdlfjlkjlksjdklfjlskdf characters, if it is sdfksjdkf99999 such a form, doped with numbers will not be
====================

The role of 11.* number
As mentioned earlier, ". *" means that the search condition is any character, the * number is not in the regular expression of any character, but the left character or the number of repetitions of the expression, * the number of repetitions is 0 or more times.
====================

12.findstr "^[0-9]*$" 2.txt
This is a match to find the pure number, such as 234234234234, if it is 2133234kkjl234 is filtered out.
Findstr "^[a-z]*$" 2.txt
This is a match to find the pure letter, such as SDFSDFSDFSDF, if it is 213sldjfkljsdlk is filtered out
If there are no * numbers in the search terms, that is, do not repeat the search conditions on the left, that is, [0-9] [A-z] that can only match the first character of the string and only this one character, because there is the beginning and end of the limit, "^[0-9]$" the first character if the number is matched, if not filtered out, If the string is 9 match, if it is 98 or 9j and so on is not OK.
=====================

The role of the expression "\<...\>"
This representation precisely looks for a string, \<sss the beginning position of the word,sss\> the end position of the word
echo Hello World computer|findstr "\<computer\>" in this form
echo Hello worldcomputer|findstr "\<computer\>" is not the form, he is looking for "computer" this string, so not.
echo Hello worldcomputer|findstr ".*computer\>" so you can match it.
=====================

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.