The egrep of the Linux base regular expression

Source: Internet
Author: User
Tags egrep

First, the basic regular expression

    • A regular expression is a pattern written by a class of special characters and text characters, some of which do not represent the literal meaning of a character, but are a function of control or wildcard that supports grep, SED, awk, vim;
    • Regular expression is a kind of grammatical rule used to describe character arrangement and matching pattern, which is mainly used for the operation of pattern segmentation, matching and finding substitution of string.
    • GREP-V Reverse Search

-I ignores case

-n Displays matching line numbers

-C Statistics match the number of rows without displaying search results

-O displays only the characters that match

-Q does not display results

(1)^: match with what begins---^ab to match lines that begin with AB;

(2)$: matches the line with what ends---ab$ represents a line that matches the end of AB;

(3)^$: match blank line---means match blank line, do not match space;

(4). : matches any single character---ab. Indicates that the match is ABC or ABC, does not match ABCD or ABCE, including spaces

(5)\: escape character, escape the special symbol------A\.B represents the line that matches the A.B, the actual property of the escape point, and does not represent any character;

(6)*: match the preceding item 0 or more times---ab* means match a or AB or B or ABB, as long as one or more lines match the AB character;

(7). *: matches any character---ab.* means match ab or either ABC or ABD, including spaces;

(8)[]: matches any single character within the set---AB[CD] means match ABC or ABC, does not match Abe or ABF;

(9)[^]: matches any single character other than the set---AB[^CD] b means match Abe or ABF, does not match abc,abd;

1. Expression test Data

[email protected]/]# cat test
I am Oldboy Teacher!
Iteach Linux.


I like Badminton Ball,billard Ball and Chinese chess.

My blog is http//:oldboy.blog.51cto.com
Our site is http://www.etiantian.org

My QQ num is 49000048


Not 4900000048
My god,i am not oldbey,but Oldboy

Oldb y

(1) ^: match with what begins

[[email protected]/]# grep "^m" test
My blog is http//:oldboy.blog.51cto.com
My QQ num is 49000048
My god,i am not oldbey,but Oldboy

(2) $: match the line with what ends

[[email protected]/]# grep "m$" test
My blog is http//:oldboy.blog.51cto.com

(3) ^$: Match blank line (use-VN parameter to troubleshoot blank lines, show only non-blank lines)

[Email protected]/]# grep-vn "^$" test
1:i am Oldboy Teacher!
2:iteach Linux.
5:i like Badminton Ball,billard Ball and Chinese chess.
7:my Blog is http//:oldboy.blog.51cto.com
8:our site is http://www.etiantian.org
12:my QQ num is 49000048
15:not 4900000048
16:my god,i am not oldbey,but Oldboy
18:oldb y

(4). : Matches any single character

[[email protected]/]# grep "M." Test
I am Oldboy Teacher!
I like Badminton Ball,billard Ball and Chinese chess.
My blog is http//:oldboy.blog.51cto.com
My QQ num is 49000048
My god,i am not oldbey,but Oldboy

(5) \: Escape character, escape special symbol

[Email protected]/]# grep "\.$" Test #以点结尾的行
Iteach Linux.
I like Badminton Ball,billard Ball and Chinese chess.

(6) *: Matches the previous item 0 or more times

[Email protected]/]# grep-o "0*" Test #精确匹配显示匹配的数据
0000
000000

(7). *: Matches any character

[[email protected]/]# grep ". *" Test all matches, including spaces
I am Oldboy Teacher!
Iteach Linux.


I like Badminton Ball,billard Ball and Chinese chess.

My blog is http//:oldboy.blog.51cto.com
Our site is http://www.etiantian.org

My QQ num is 49000048


Not 4900000048
My god,i am not oldbey,but Oldboy

Oldb y

(8) []: matches any single character within the set

[[email protected]/]# grep "[0-9]" test #匹配有数字的行
My blog is http//:oldboy.blog.51cto.com
My QQ num is 49000048
Not 4900000048

(9) [^]: matches any single character other than the set

[[email protected]/]# grep "[^0-9]" test #匹配非数字的行;
I am Oldboy Teacher!
Iteach Linux.
I like Badminton Ball,billard Ball and Chinese chess.
My blog is http//:oldboy.blog.51cto.com
Our site is http://www.etiantian.org
My QQ num is 49000048
Not 4900000048
My god,i am not oldbey,but Oldboy
Oldb y

Second, extended regular expression

Test data

[email protected] ~]# cat Test1
I am Oldboy Teacher!
Iteach Linux.


I like Badminton Ball,billard Ball and Chinese chess.

My blog is http//:oldboy.blog.51cto.com
Our site is http://www.etiantian.org

My QQ num is 49000048


Not 4900000048
My god,i am not oldbey,but Oldboy

Good
Goood

Gd

(1) +: match the previous 1 or more---ab+ indicates a match AB or ABB, does not match A;

[Email protected] ~]# grep-e "Go+d" test1
My god,i am not oldbey,but Oldboy
Good
Goood

(2)? : Match the previous 0 or 1 times---ab? Indicates matching A or AB, does not match ABB;

[Email protected] ~]# grep-e "Go?d" test1
My god,i am not oldbey,but Oldboy
Gd

(3) | : Match any of the---ab|cd to match ab or CD;

[Email protected] ~]# grep-e "God|good" test1
My god,i am not oldbey,but Oldboy
Good

(4) (): Match Expression---A (CD) e indicates matching Ace or ADE, does not match AE;

[[email protected] ~]# grep-e "G (a|oo) d" test1
Good

[Email protected] ~]# grep-e "G (OO) d" test1
Good
Gd

(5) {n,m}: Matches the previous item n~m times---ab{2,3} indicates matching ABB or ABBB, and {} needs to be escaped;

[[email protected] ~]# grep-e "0{3,5}" test1
My QQ num is 49000048
Not 4900000048

(6) {n,}: matches the preceding item at least n times, contains n times---ab{2,} indicates matching ABB or ABBB, need to escape {};

[Email protected] ~]# grep-e "0{3,}" test1
My QQ num is 49000048
Not 4900000048

(7) {n}: matches the preceding item n times---ab{2} matches ABB, need to escape {};

[[email protected] ~]# grep-e "O{1}" test1
My god,i am not oldbey,but Oldboy

(8) {, m}: matches the preceding item up to M times, contains m times,---ab{,2} matches a or AB or ABB, need to escape {};

The egrep of the Linux base 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.