The grep of the "Three Musketeers" in the text processing of Linux system

Source: Internet
Author: User
Tags modifier modifiers

When we write a script, we often intercept some data from the command output, such as: I want to know how many hard disks are on the host

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/3C/B7/wKioL1PCeFLxxHMSAABsWZrIneo226.jpg "title=" Displays the device for the host. png "alt=" wkiol1pceflxxhmsaabswzrineo226.jpg "/>

this is very convenient. Using grep to find the data we want and then using sed , or Cup cutting, we can get the device name of our host, which is a way for us to fetch the data.

Egreq is an upgraded version of grep, supports extended expressions, andFgrep does not support regular expressions.

Use format:

grep [OPTIONS] PATTERN [FILE ...] -O print output matches to string-I do not match when ignoring case-V printout mismatch Content-E indicates support for extended expressions

"grep" is a text-search tool that reads text lines by line , matches the given pattern (pattern), and looks at whether the word/character in the row matches the pattern. The rows that are matched by default are output to the monitor.

The program generally has data input and output, also known as the program's IO. From this perspective,the input of the GREP program data can be either a file ( reading data from disk ) or a standard input (that is, it can use the pipeline as a data input for grep. )

When you use the "grep" command, you do not specify a file, it reads the data from the standard input.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/3C/B7/wKioL1PCeTCTeTDaAABB7Bd6Rec533.jpg "title=" 1.png " alt= "Wkiol1pcetctetdaaabb7bd6rec533.jpg"/>

"grep" defaults to the output of rows matching the pattern to the standard output.

Grep is a text search tool that only handles text. So, when we are looking for a directory file, we first use "ls" to list thecontents of the directory, and then use grep to do text search processing.

catalog file Search, using the metacharacters in the Bash shell.

Simply understand the difference between a file wildcard and a regular expression?

lists files in the/etc directory that end with. conf or . D.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/3C/B7/wKiom1PCebWjTwGEAAGWd6yB1rs834.jpg "title=" 2.png " alt= "Wkiom1pcebwjtwgeaagwd6yb1rs834.jpg"/>

is to do the character ( string ) match, or do the words match?

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/3C/B7/wKioL1PCedWj8C6FAAH1pvUpt9s102.jpg "title=" 3.png " alt= "Wkiol1pcedwj8c6faah1pvupt9s102.jpg"/>

with the ending anchor, grep is only available if the ending of the word matches the given "pattern" . If the word

The middle or front matches the pattern,and grep is not displayed.

The aliases.db file is not displayed when the following matching pattern is added to the ending anchor .

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/3C/B7/wKiom1PCekfAUQVxAAI6nV0HyD0809.jpg "title=" 3.png " alt= "Wkiom1pcekfauqvxaai6nv0hyd0809.jpg"/>

Summarize:

When using "grep", note that the wildcard wildcards regular of a file may appear as many meta-characters of the table, but the special meaning they represent is different. It's easy to confuse.

When using "grep" To search for text, the analysis idea summarizes:

1 . Analysis of matching content

is it a word match or a character? ( string ) match it.

A, if the matched content is a word, you need to use the first anchor (\<) and ending anchors (\>) The string that identifies the word that is written in the pattern is not a literal, but a word.

B, if the matched content is a character ( string ) , you can simply make the match as grep matching mode.

That is, string search.

2 . Determine the location of the match

Is it the beginning or the end of the line, the head of the word, or the ending, or any position

A, Regular expressions provide a number of metacharacters that indicate where to match in a line of text.

such as: The beginning of the anchor character (^) , end-of-line anchor ($) , the first anchor of the word (\<) , ending anchor (\>) These are collectively referred to as anchors ( Target Point ) .

B, If there are no such anchors in the regular expression, the position that the regular expression matches in a line of text is the character ( string ) .

3 , do you want to modify the matching content?

They are used to expand or shrink ( that is, it has been modified ) The regular expression matches the range of the text line. Modifiers include an asterisk (*), parentheses (),

Backslash symbol, question mark. These can all be called modifiers. is used to modify the matching content.


Since our matches are divided into words and characters, it is necessary to know what the word is in grep.

Understanding The so-called words in grep is useful for us to be familiar with using this command to do a text search.

comprehension of words defined by "grep"

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/3C/B7/wKioL1PCe2HjQMh4AACU1T6iGDQ056.jpg "title=" 3.png " alt= "Wkiol1pce2hjqmh4aacu1t6igdq056.jpg"/>

Grep defines the word:

Word: A string of words that consists of consecutive characters that do not contain special characters.

as shown, we use "grep" to search for the word save in the text B.txt. It's good to know what the so-called "word" is in the grep world. The red is the save word shown in the figure .

"continuous" means: there are no spaces between the strings, and the string has no special characters ( brackets , curly braces , question marks, etc. ) . It can also be said that special characters are the boundaries of words.


So how do you use the "grep" search text in an implementation app tofind what we need?

1. "grep" matches the character.

Analysis: Is there a requirement to make a matching position? No

Find the line containing the character s without modification.

So, we can write:grep "S" FILE

The position of a regular expression in a line of text to match is a character. Therefore, each character in the text line is matched with a regular expression.

The Grep command reads a row, and each character in the row matches the pattern.

Displays the character to which the highlight matches if it matches. As shown in.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/3C/B7/wKioL1PCe8KADGxvAACbPHZLNV8379.jpg "title=" 3.png " alt= "Wkiol1pce8kadgxvaacbphzlnv8379.jpg"/>

Example: Finding a line with one or 0 s characters in the AB character in the file A.txt

the match is a character, so it is not necessary to identify the first anchor and the ending anchor.

There is no requirement to match the location, so you do not need to use positional anchors.

match Content The s character between AB cannot be determined, so use the modifier (?) . .

question Mark (?) , modifier: Represents the number of occurrences of the preceding character as 0 or 1 times . As shown in the following:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/3C/B7/wKiom1PCfE2QrY1bAADPztN0gUI641.jpg "title=" 3.png " alt= "Wkiom1pcfe2qry1baadpztn0gui641.jpg"/>

analysis, we do character matching,and grep takes a match with a given pattern instead of a word, but a character.

here,grep reads each character to match the pattern. See if the character read is a , if it is a character A, the word a specifier is a or no character s , if the a character is followed by a special character (\?). , and the next character obtained is the character B, which means that the character a to character B is just being read , and this string matches the given pattern .

2. "grep" does the word match.

Find The row that contains the word save.

Analysis:

The matching content is "word", so use the first anchor and the ending anchor to identify the word.

There is no requirement to match the location, so you do not need to use positional anchors.

The regular expression in a line of text to match the position of the word. Whether it's the first anchor or the ending anchor. Obviously,grep matches a "word" and "pattern" every time, and the match is highlighted with a regular expression,

A word is identified by using the "first anchor \<" and the "ending anchor character \>".


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/3C/B7/wKioL1PCfIuwyHzaAADHKrE4tAc853.jpg "title=" 3.png " alt= "Wkiol1pcfiuwyhzaaadhkre4tac853.jpg"/>

3. "grep" does a line match . The regular expression in a line of text to match the position of the end or beginning of the line.

Our match has "word match" and "character match", so the criteria we specify can be:

A, a line that starts with a character

B, a line that ends with a character

C, A line that starts with a word

D, line ending with a word

analysis, as shown in. Find the line that starts with the word save .

The first step is to match the row that contains the word save .

in the second step, using the line anchor ^ to tell grep that we are doing a row-matching operation, so

The second time you use grep to do a text search, only the lines that start with the word save are displayed.

Attention:

When we use grep for text Search, pay special attention to whitespace, especially at the end of the line. As shown in the following:

The first time you searched with grep, there was no line to match.

second hypothesis string LISTEN followed by a space and then a line, the search for a matching line.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/3C/B7/wKiom1PCfWzh8nBeAAEyZy1jbIM068.jpg "title=" 3.png " alt= "Wkiom1pcfwzh8nbeaaeyzy1jbim068.jpg"/>

This article is from the "Linux" blog, so be sure to keep this source http://9528du.blog.51cto.com/8979089/1437658

Related Article

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.