Shell Script-Regular expression

Source: Internet
Author: User
Tags character classes egrep

character class (Character Class): The above example x and y, which represent one character in the pattern , but the range of values is a class of words

Any one of them.

Quantity Qualifier (quantifier): each part of a mail address can have one or more x character , IP each part of the address

can have 1-3 a y character

(Anchor) : Describes the positional relationships between the various character classes and ordinary characters, such as e-mail addresses that are divided into three

section , @ and . separate ,ip address four parts with . separate

description. In order to represent positional relationships , the concept of positional qualifiers is required , as described below.

specify some special syntax to represent character classes, quantifier and positional relationships , then use these special syntax and ordinary characters

together represents a pattern , that's the regular expression . (Regular Expression) .


Note the regular expression parameter with encloses the , shell

it has a special meaning . ( e.g. \), It is only possible to enclose these characters in single quotes to ensure that they are passed intact . grep command , and not

will be Shell explain it out.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/86/49/wKioL1e6_NGABdgFAAGXhY9tOkM070.png "style=" float: none; "title=" character class. png "alt=" Wkiol1e6_ngabdgfaagxhy9tokm070.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/86/4A/wKiom1e6_NKzGH3vAALEPKyHixI085.png "style=" float: none; the "title=" position qualifier. png "alt=" Wkiom1e6_nkzgh3vaalepkyhixi085.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/86/49/wKioL1e6_NODwbuKAAFqlFX_ZDY142.png "style=" float: none; "Title=" other special characters. png "alt=" Wkiol1e6_nodwbukaafqlfx_zdy142.png "/>

The above introduction is grepof the regular expressionExtendedSpecification, BasicThe specification also has these grammars, just characters ? +{}| () should be

interpreted as normal character ,to indicate the above special meaning, you need to add \escaped. If you usegrepand not Egrep,and does not add- e

Parameters , you should follow Basic specification to write regular expressions. (Be sure to pay attention to!!!!! )



Convenient here is to rename the command here

Alias grep= ' Grep-e--colour '

Example:

[[email protected] grep]$ echo ' @ @abc ' |grep ' [@asd].ab '

@ @abc

[[email protected] grep]$ echo ' @ @abc ' |grep ' [. Asd].ab '

[[email protected] grep]$ echo ' [email protected] ' |grep ' [. Asd].ab '

[Email protected]

There are no special meanings for all the characters in this []. But outside it's a match for any one of them.


Match e-mail:

Original:

[[email protected] grep]$ cat file

[Email protected]

Dksaljdkl-sdasdas.com

[Email protected]

[Email protected]@454654

[Email protected]#@.com

[Email protected] grep]$ grep-e ' [A-za-z0-9_.-][email protected][a-za-z0-9_.-]+\. [a-za-z0-9_.-]+ ' File[email protected][email protected][email protected]@454654

More precise and ^$ limit the beginning line end

[Email protected] grep]$ grep-e ' ^[a-za-z0-9_.-][email protected][a-za-z0-9_.-]+\. [a-za-z0-9_.-]+$ ' File[email protected][email protected]


egrep Equivalent to means grep basic and

Extended two types of specifications , the difference between them is explained in the next section. There is also the fgrep command , equivalent to grep-f, which indicates that only

searching for fixed strings without searching for regular expression patterns , The following arguments are not interpreted by the syntax of the regular expression.


Matching IP

File content

192.168.11.2

192.168.1sas1.2

19as2.121.11.2

192.1sa28.11.2

192.16as1.11.2

192.111.1s1.2

192.1.1.8.12.2

192.168.2sa1.2

192.1.68.12.1

[[email protected] grep]$ grep ' [0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3} ' file192.168.11.219as2.121.11.2192.1.1.8.12.2 #grep is looking for a row that contains a pattern because it contains a pattern 192.1.1.8 so this line is also found 192.1.68.12 .1

Another way:

[[email protected] grep]$ grep ' ([0-9]{1,3}\.) {3} [0-9] {1,3} ' file192.168.11.219as2.121.11.2192.1.1.8.12.2 192.1.68.12.1

A more precise approach:

[[email protected] grep]$ grep ' ^ ([0-9]{1,3}\.) {3} [0-9] {1,3}$ ' file192.168.11.2


This article is from the "City Hunter" blog, please be sure to keep this source http://alick.blog.51cto.com/10786574/1841252

Shell Script-Regular expression

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.