On the close contact between grep and regular expression

Source: Internet
Author: User

First, grep brief

grep is a command that matches and displays the contents of Plain text in Linux according to a certain search condition . Some of the vernacular, that is, according to the user-given filter mode, the text of the content to match and display.

The usage format of grep:

grep [OPTIONS] PATTERN [FILE]

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/41/wKiom1bjrqiTMvqRAAATYWKbdt8152.png "title=" 1.png " Width= "628" height= "119" border= "0" hspace= "0" vspace= "0" style= "WIDTH:628PX;HEIGHT:119PX;" alt= " Wkiom1bjrqitmvqraaatywkbdt8152.png "/>

Where options represent the parameters available for the command, pattern represents the search criteria, where we combine regular expressions, and file represents the text object to manipulate.


Second, grep common parameters analysis

For example, we want to search for a line containing a "mail" string under/ETC/PASSWD, with the following result:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/41/wKiom1bjtIjSnFDhAAAXt1-EMLQ190.png "title=" 2.png " alt= "Wkiom1bjtijsnfdhaaaxt1-emlq190.png" width= "628" height= "119" border= "0" hspace= "0" vspace= "0" style= "width : 628px;height:119px; "/>

The red highlight is the result we matched.

Note that grep defaults to matching the row output, rather than displaying the matching string itself.


(1)- o: indicates that only the characters that match are displayed .

We add the "-o" parameter, what will the result be?

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/41/wKiom1bjti6gInPcAAAVMQtJRk8421.png "title=" 3.png " alt= "Wkiom1bjti6ginpcaaavmqtjrk8421.png"/>

Just display the matching characters, which is the function of "-O".


(2) - V: Indicates that a reverse match is displayed

Searching for the "mail" string in/etc/passwd, plus the "-V" parameter, will show what is going to match the string; Syntax: grep-v "Mail"/etc/passwd

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/3F/wKioL1bjujyhaH0VAAAfGybG_z4314.png "title=" 4.png " alt= "Wkiol1bjujyhah0vaaafgybg_z4314.png"/>

(3) -E: Represents a regular expression that uses an extension

Adding this parameter is the same as using egrep, which we'll talk about later in the next article.


(4)-I : Ignore letter Case

We searched for the "mail" string in/TMP/PASSWD and the results were as follows:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/41/wKiom1bjvTPSrPQJAAAai7ynk-8087.png "title=" 5.png " alt= "Wkiom1bjvtpsrpqjaaaai7ynk-8087.png"/>

Both lowercase and uppercase are displayed.


Third, the use of regular expressions

Regular expressions are made up of a number of meta-characters that can be more accurately described in terms of matching conditions and are very powerful.


Its organizational structure has three main points: the number of occurrences of a particular position-the character to be matched- we analyze each of these three points.


Basic regular Expression meta-characters:

I . character matches ( characters to match ):

(1).: The dot number indicates matching any single character;

For example, find the contents of/tmp/passwd with three characters followed by a G, as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/40/wKioL1bjznuSMkmHAAAiUFbNV3I839.png "title=" 6.png " Width= "628" height= "119" border= "0" hspace= "0" vspace= "0" style= "WIDTH:628PX;HEIGHT:119PX;" alt= " Wkiol1bjznusmkmhaaaiufbnv3i839.png "/>


(2)[]: matches any single character within the parentheses

For example, look for the characters in/tmp/passwd followed by B or P, starting with a, as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7D/42/wKiom1bj0BmCi1qIAAAaeg4Pu1w182.png "title=" 7.png " alt= "Wkiom1bj0bmci1qiaaaaeg4pu1w182.png"/>


(3) special meaning of the character : [: Upper:] denotes uppercase, [: lower:] denotes lowercase letters, [:d igit:] denotes numbers; [: Alpha:] denotes all letters; [: Alnum:] denotes all letters and numbers; [: space:] [: blank:] denotes spaces and tabs; [:p unct:] denotes all punctuation marks;


For example, to find the contents of any punctuation mark that begins with 76 in/TMP/PASSWD, do the following:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/42/wKiom1bj1_mzSXkjAAAa3_JRq_c904.png "title=" 8.png " alt= "Wkiom1bj1_mzsxkjaaaa3_jrq_c904.png" width= "627" height= "119" border= "0" hspace= "0" vspace= "0" style= "width : 627px;height:119px; "/>


(4)[^]: represents any single character outside of the matching range

For example, find the contents of the/tmp/passwd followed by s that are not alphabetic, as follows:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7D/41/wKioL1bj2wyDw6GuAAAgKIAANwc104.png "title=" 9.png " alt= "Wkiol1bj2wydw6guaaagkiaanwc104.png" width= "628" height= "119" border= "0" hspace= "0" vspace= "0" style= "width : 628px;height:119px; "/>


II: Number of matches ( number of occurrences): Indicates the number of times the preceding characters are matched, and the symbols appear after the characters that are to be matched.


(1)*: to match the preceding character 0 or more times, it can also be understood that the value range is 0-infinity.

For example: "A*b" indicates that a appears 0 or more times, the result is as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/43/wKiom1bj3-jifwPBAAAViGjvOd4455.png "title=" 10.png "alt=" Wkiom1bj3-jifwpbaaavigjvod4455.png "width=" 627 "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 627px;height:119px; "/>


(2). *: Any character of any length, the asterisk is decorated with the preceding point number .

For example: "Al.*b" means that between Al and B no matter how long the character can be;

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7D/42/wKioL1bj7bKDsamRAAAhx4wd1ug757.png "title=" 11.png "alt=" Wkiol1bj7bkdsamraaahx4wd1ug757.png "width=" 628 "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 628px;height:119px; "/>


(3)\+: Indicates that the previous character of the match appears at least 1 times, which can be understood as the value range is 1-infinity.

For example: "A\+b" means that a at least one occurrence, more than unlimited; compared with "a*b", what difference does it find?

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/42/wKioL1bj71LzS9w1AAAWcAL-Wss652.png "title=" 12.png "alt=" Wkiol1bj71lzs9w1aaawcal-wss652.png "width=" 628 "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 628px;height:119px; "/>


(4)\?: to match the preceding character 0 or 1 times ; say it either once or not.

For example: "A\?b" means only B and AB can match;

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/42/wKioL1bj8sqD8JwaAAAUbtFSEsc918.png "title=" 13.png "alt=" Wkiol1bj8sqd8jwaaaaubtfsesc918.png "width=" 628 "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 628px;height:119px; "/>


(5)\{m\}: Indicates that the preceding character appears m times and M is a positive integer

For example: "A\{2\}" indicates that a can only occur 2 times, as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7D/44/wKiom1bj84zgQoLMAAAX7Osdv6k961.png "title=" 14.png "alt=" Wkiom1bj84zgqolmaaax7osdv6k961.png "width=" 628 "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 628px;height:119px; "/>


(6)\{m,n\} indicates that it appears at least m times, at most n times;

For example: "A\{2,3\}" indicates that a has occurred at least 2 times, up to 3 times

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/44/wKiom1bj9W7CyCGCAAAV5xvl0yw244.png "title=" 15.png "alt=" Wkiom1bj9w7cycgcaaav5xvl0yw244.png "width=" 628 "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 628px;height:119px; "/>



Iii. Position anchoring (at a specific location): qualifying where the matched character appears


(1)^: The beginning of the line is anchored and appears on the left side of the matching character

For example: "^ab" indicates that the contents of the line beginning with AB are matched, as follows:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/42/wKioL1bj_OnBIM4XAAAXoAvsjjU154.png "title=" 16.png "alt=" Wkiol1bj_onbim4xaaaxoavsjju154.png "width=" 628 "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 628px;height:119px; "/>


(2)$: Line end anchoring, appearing on the right side of the matching character

For example: "ab$" means matching the content with AB as the end of the line, as follows:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/42/wKioL1bj_bHD9h-YAAAXzgM4yZ0295.png "title=" 17.png "alt=" Wkiol1bj_bhd9h-yaaaxzgm4yz0295.png "width=" 628 "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 628px;height:119px; "/>


(3)^$: blank line


(4)\< or \b: The first anchor of the word, used to match the left side of the word

For example: "\<b." Represents a word that matches the following 2 characters, preceded by B, with the following actions:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/44/wKiom1bj_1KRmFoXAAAZaNd6QQw970.png "title=" 18.png "alt=" Wkiom1bj_1krmfoxaaazand6qqw970.png "width=" 628 "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 628px;height:119px; "/>


(5)\> or \b: The ending anchor to match the right side of the word

For example: "dd\>" means matching a word with DD ending with the following actions:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/44/wKiom1bkAW2DijeqAAAaHGuwF_o408.png "title=" 19.png "alt=" Wkiom1bkaw2dijeqaaaahguwf_o408.png "width=" 628 "height=" 119 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 628px;height:119px; "/>











This article comes from "people, born and lonely." "Blog, be sure to keep this provenance http://amani.blog.51cto.com/11305400/1750340

On the close contact between grep and regular expression

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.