Batch Processing Regular Expression (findstr) sorting

Source: Internet
Author: User

Syntax
Findstr [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/n] [/m] [/o] [/P] [/offline] [/G: file] [/F: file] [/C: String] [/D: dirlist] [/A: colorattribute] [Strings] [[drive:] [path] filename [...]

Parameters

/B
Match the pattern if it is at the beginning of the row.

/E
If it is at the end of the row, it matches the pattern.

/L
Search for strings by word.

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

/S
Search for matched files in the current directory and all subdirectories.

/I
The specified search is case insensitive.

/X
Print the matched rows.

/V
Print only the rows that do not contain a match.

/N
Print the row number before each matched row.

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

/O
Print the search offset before each matching row.

/P
Skip a file that contains non-printable characters.

/Offline
Use offline properties to process files.

/F: File
Reads the file list from a specified file.

/C: String
Use the specified text as a text search string.

/G: File
Obtain the search string from the specified file.

/D: dirlist
Search for a list of directories separated by commas.

/A: colorattribute
Specify the color attribute using two hexadecimal numbers.

Strings
Specify the text to be searched in filename.

[Drive:] [path] filename [...]

Specifies the file to be searched.

/?
Display help at a command prompt.

Note

• Use strings and [drive:] [path] filename [...]
In the command string, all findstr command line options must be before strings and [drive:] [path] filename.

• Use Regular Expressions in findstr
Findstr can precisely search for the text in any ASCII file or file. However, sometimes only a portion of the information to be matched or a wider range of information needs to be found. In this case, findstr has the powerful function of searching various texts using regular expressions.

A regular expression is a symbol used to specify the text type, which is opposite to a precise string. Use text characters and metacharacters. Each character that has no special meaning in regular expression syntax is a character that matches the character that appears. For example, letters and numbers are text symbols. Metacharacters are symbols with special meanings (operators or delimiters) in the regular expression syntax.

The following table lists metacharacters accepted by findstr.

Character Value

.
Wildcard: any character

*
Repeated: Previous zero or multiple characters or classes

^
Position in the row: Beginning of the row

$
Position in the row: end of the row

[Class]
Character class: any character in the Set

[^ Class]
Reverse class: any character that is not in the Set

[X-y]
Range: any character in the specified range

\ X
Escape: Text usage of metacharacters x

\ Word position: Beginning

XYZ \>
Word position: End

Special characters used in combination with regular expression syntax are very powerful. For example, the combination of the following wildcard (.) and repeat (*) can match any string:

.*

Use the following expression as an integral part of a larger expression that matches any string starting with "B" and ending with "ing:

B. * ing

Example
Use spaces to separate multiple search strings, unless the parameter is 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" (W uppercase letters) in the file proposal.txt, type:

Findstr windows proposal.txt

To search for the current directory containing the word windows and each file in all subdirectories (case-insensitive), type:

Findstr/S/I windows *.*

To search for all rows containing the word "for" (the front can contain any space, such as: ComputerProgramAnd include the row 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 of files, create a text file containing each search standard on the new line. You can also list the exact files to be searched in text files. To use the search criteria in the finddata.txt file to search for the files listed in filelist.txt and save the results to the file results. Out, type:

Findstr/G: finddata.txt/F: filelist.txt> results. Out

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

Findstr/S/I/M "\"*.*

It is assumed that not only the word "computer" needs to be searched, but also all other words starting with the letter "comp", such as "compliment" and "compete ". Enter:

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
Search for arbitrary characters from file 2.txt, excluding empty characters or empty lines
================================

2. findstr. * 2.txt or findstr ". *" 2.txt
Search for arbitrary characters including empty lines and empty characters from file 2.txt
================================

3. findstr "[0-9]" 2.txt
Search for strings or rows containing numbers 0-9 from file 2.txt
================================

4. findstr "[A-Za-Z]" 2.txt
Search for strings or rows containing any characters from file 2.txt
================================

5. findstr "[abcezy]" 2.txt
Search for strings or rows containing letters a B C E Z Y from file 2.txt
================================

6. findstr "[A-fl-Z]" 2.txt
Find the string of lowercase character a-f l-Z from file 2.txt, but it does not contain the letters g h I j k.
================================

7. findstr "m [ABC] [hig] Y" 2.txt
From file 2.txt, you can match Mahy, mbiy, Mahy, etc .....
================================

8. ^ and $ symbols
^ Indicates the beginning of the line. "^ step" matches only the first word in "Step Hello World ".
$ Indicates the end of a row. "Step $" matches only the last word in "Hello world step ".
================================

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

10. findstr "[^ A-Z]" 2.txt
Same as above, if it is a string or line of pure letters, it will be filtered out, such as sdlfjlkjlksjdklfjlskdf, if it is sdfksjdkf99999, it will not be mixed with numbers
================================

11. *
As mentioned above ". * "indicates that the search condition is any character. * indicates the number of repetitions of the character or expression on the left rather than any character in the regular expression, * indicates that the number of duplicates is zero or multiple.
================================

12. findstr "^ [0-9] * $" 2.txt
This is a pure number that is matched, for example, 234234234234. If it is 20173234kkjl234, it will be filtered out.
Findstr "^ [A-Z] * $" 2.txt
This is a pure matching letter, such as sdfsdfsdfsdf. If it is 213sldjfkljsdlk, it will be filtered out.
If there is no * in the search condition, that is, the Search Condition on the left is not repeated, that is, [0-9] [A-Z] can only match the first character of a string. This is because of the limitations on the beginning and end of a line, "^ [0-9] $" match the first character if it is a number. If not, filter it out. If the string is 9, match, if it is 98 or 9j, you cannot.
==================================

13. "\ <... \> "Function of this expression
This represents exact search for a string, \ indicates the end position of a word
echo Hello world computer | findstr "\ " in the form of
echo Hello worldcomputer | findstr "\ "this form cannot be, he is looking for the "computer" string, so no.
echo Hello worldcomputer | findstr ". * computer \> "to match
=================================

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.