String SEARCH cmd find command

Source: Internet
Author: User

I am so dependent on findstr that when I explain the find command to you, I have to honestly find/? In the CMD window /? This command, and press the Enter key heavily.

Find /? The execution results are very fast. after a crash, all the help information is provided. Compared with more than 10 switches of the findstr command, find has only five switches, which makes it easy to understand.

The basic format of the find command is as follows: find "the string to be searched" the file to be searched for. Here, the content of "the string to be searched" is indispensable, quotation marks are also required. For example, find "bathome" D: \ test.txt. Find all rows containing the string "bathome" in the test.txt file of the D-root directory.

The find result may be in two cases:

1. When the file contains the string to be searched, the entire line of content in the position of the string is returned. The specific format is: the first line is "---------- ×××" (××× indicates the file name to be searched, excluding quotation marks). The second line begins to show the entire line of content containing the string;
2. When no string is found in the file, the find result returns only the line "---------- ×××" (××× indicates the file name to be searched, excluding quotation marks ).

When searching for a specified string, some cases need to be case sensitive, while some cases do not care about Case sensitivity. How can we meet these requirements?

It's easy to add or cancel the/I switch during the find operation. The specific usage is: Find/I "ABC" test.txt, which indicates that it is case insensitive, the text content contains ABC, and so on. Find "ABC" test.txt is strictly case sensitive and can only match ABC, but cannot match ABC, ABC and so on.

Sometimes, our requirement is not to find a specific string, but to detect which rows do not contain a specific string. In this case, you can use the switch/V for usage: find/V "ABC" test.txt, which indicates to search for rows without the string ABC (ABC is case sensitive). If ABC is case insensitive, it should be written as find/I/V "ABC" test.txt.

Sometimes, our requirement is very simple. We just want to count the total number of rows containing a string. In this case, we should use the/C switch and write it as find/C "ABC" test.txt.

What should I do if I want to display the row number at the beginning of each row? Write it as find/N "ABC" test.txt, and then the contents of test.txt will be displayed. Different from the original text, a line number is added at the beginning of each line.-Note, it only adds a row number to the display and does not actually rewrite the file content.

If you see this command: Find/I/V/N "ABC" test.txt, please feel down first and never be dazzled by many switches, based on the above explanation, I believe that you will soon understand its function: In the test.txt file, find all rows that do not contain the string ABC and mark the row at the beginning, when searching, the string ABC is case insensitive.

If you use this statement: Find/C/N "ABC" test.txt, what kind of result will it show? The answer is: Switch/n will be ignored. The final result will not show the row content with a row number, but will only show the total number of matched rows.

Echo abcdef | find "ABC". Have you ever seen this method? The meaning is: in the string abcdef, check whether the string ABC exists. That is, find supports the pipeline symbol, and uses the execution result of the statement before the pipeline symbol as the search object of the specified string, you can even use the following statement: Find "ABC" test.txt | find "XYZ". Then, locate the rows with strings ABC and XYZ in test.txt, you can continue to use the pipeline symbol to connect this find statement. We call this process multi-filter.

As we have said before, in the command statement "find" the string to be searched "for the file to be searched, quotation marks are required. If the content we are looking for is the double quotation marks themselves, what should I do?

The answer is simple: escape the double quotation marks first with double quotation marks, and put them in the pair of double quotation marks to write the "find" test.txt format. If you want to find two consecutive double quotation marks, it should be written as find "test.txt.

If you want to find the character blocks between two lines, I suggest you give up this crazy idea early, because find only targets strings on the same line and cannot find cross-line character blocks.

Compared with the findstr command, the find function is indeed much more limited. Many people regard it as a chicken rib because it does not support regular expressions, so it is relentlessly abandoned.

However, find is not useless. It earned a place in the competition with findstr by virtue of its unique skills.

What are these unique skills? (Thanks to the reminder from batcoder and the supplement from zqz0012005)

1. Count the total number of rows containing the specified string. Find/C "ABC" test.txtyou can calculate the total number of lines containing the string ABC in test.txt, and findstr does not provide this function directly, which must be implemented with the for statement;
2. Find can read Unicode text, but findstr cannot;
3. Find can filter some special characters, but findstr cannot. For example, when we use the fsutil fsinfo drives statement to query disk partitions, if you want to display the drive letter branches rather than on the same line (this is useful when you use the for statement to extract the drive letter), find can show its talents, while findstr can only be stunned, specific statement: fsutil fsinfo drives | find/V ""

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.