String lookup cmd Find command _dos/bat

Source: Internet
Author: User
Tags switches
I was so dependent on findstr that when I explained find command, I had to honestly knock find in the cmd window. This command, and then hit the ENTER key heavily.

Find/? The implementation of the results quickly, a bit, help information out, and findstr command more than 10 switches, find only 5 switches, is dwarfed.

The basic format of the Find command is this: "Find the string" to look for the file, where the "string to find" is essential, and quotation marks are required. For example: Find "Bathome" D:\test.txt, which indicates that all rows containing the Bathome string are found in the text file Test.txt in the D-packing directory.

The results of find will be in two ways:

1, when the file contains the string to find, it returns the entire row of the string, in the form that the first line is "----------xxx" (XXX indicates the name of the file being searched, excluding the quotation marks), and the second line begins to appear with the entire line containing the string;
2, when the file does not appear to look for the string, find results only return the "----------xxx" line (XXX represents the file name found, not including quotation marks).

When looking for a specified string, some cases need to be case-sensitive, while others do not care about the case, how do you implement these requirements?

Very simply, in Find, add or cancel/I switch is, the specific usage is: find/i "abc" test.txt means case-insensitive, text content contains ABC, ABC, ABC and so on; find "ABC" Test.txt is strictly case-sensitive and matches only ABC, not characters such as ABC or ABC.

Sometimes our requirements are not to find a string, but to detect which rows do not contain a particular string, and at this point, switch/V is used: find/v "ABC" Test.txt, which represents the search for rows that do not contain the string ABC (ABC to be case-sensitive), should be written as find/i/V "ABC" test.txt if it does not differentiate between the case of ABC.

Sometimes, our request is very simple, just want to count the total number of lines containing a string, this time, should use switch/C, written find/c "abc" test.txt.

What if you want to display line numbers at the beginning of each line? Then write find/n "abc" test.txt, it will show the contents of the Test.txt, unlike the original text, in the beginning of each line of content, plus the line number-note that it is only in the display of the line number only, and does not really overwrite the contents of the file.

If you see such a command: find/i/v/n "ABC" Test.txt, please take a little bit of patience first, don't be dazzled by the multitude of switches, in close contrast to the above explanation, I believe you will soon understand its function: in the Test.txt file, look for all lines that do not contain the string ABC, and at the beginning of the line mark the line number of these lines, when looking, the string ABC is case-insensitive.

What would happen if you used such a statement: FIND/C/n "abc" Test.txt? The answer is: Switch/n will be ignored, the end result will not show the line with line number, only the number of rows to display matching.

echo Abcdef|find "abc", Have you seen this kind of writing? It means: in string abcdef, detect the existence of the string ABC, that is, find supports the piping symbol, the execution result of the statement before the pipe symbol, as the lookup object of the specified string, or even, you can use such a statement: finding "abc" test.txt| Find "XYZ", which represents a line in Test.txt that exists with both the string ABC and the string xyz, so long as you like, you can continue to connect this find statement with the pipe notation, which we call a multiple filter.

In the front, we said, in the command statement that finds the file to find, the quotation marks are required, what if we are looking for the double quotation mark itself?

The answer is simple: The double quotes are first escaped with double quotes, and then placed in double quotation marks, written in Find "" "" test.txt format, if you want to look for two consecutive double quotes, you should be written "" "" "" "Test.txt.

If you want to find a block of characters between two lines, I advise you to give up this crazy idea as early as possible, because, find a character block that spans rows only for strings that are on the same line.

Compared with the findstr command, find has a much more limited function, just because the regular expression is not supported, there are many people treat it as a chicken, and thus be mercilessly abandoned.

However, find is not useless, by virtue of their unique skills, in the competition with FINDSTR, it earned a place.

What are these unique stunts? (Thanks to Batcoder's reminders and zqz0012005 's additions)

1, statistics with the specified string of the total number of rows. The FIND/C "ABC" Test.txt can be used to count the total number of rows in the test.txt containing the string ABC, while FINDSTR does not provide the function directly and needs to be implemented with a for statement;
2, find can read the text in Unicode format, and findstr is not;
3, find can filter some special characters, and findstr is not, for example, we use the fsutil fsinfo drives statement to query the disk partition, if you want to let the letter branch display rather than display on the same line (this is useful when extracting the letter with the For statement) , find can be a skill, and findstr can only stare, the specific statement is: 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.