The grep of the Linux text processing weapon

Source: Internet
Author: User
Tags perl regular expression egrep

First, what is grep?

Grep:global Search Regular expression (RE) and print out of the line, fully searching for regular expressions and printing them out, is a command-line tool that can find strings from text or streams, It can be more flexible by using regular expressions to find a string, that is, through complex patterns to find to meet your needs.

Ii. Classification of regular expressions

1. Basic Regular expression: BRE (basic REGULAR expression)

2, expand the Regular expression: ERE (EXTEND REGULAR expression)

3. Perl Regular Expression: PRE (Perl REGULAR expression)

Iii. features of grep,sed, and awk families

1, grep, egrep, fgrep the characteristics of the regular

1) grep supports BRE, ERE, PRE

The A.GREP directive does not follow any parameters, which means using the BRE

The b.grep instruction is followed by the-e parameter, which indicates the use of Ere

The c.grep instruction followed by the-p parameter indicates the use of the pre

2) egrep support Ere, PRE

The A.EGREP directive does not follow any parameters, which indicates the use of Ere

The b.egrep instruction followed by the-p parameter indicates the use of the pre

3) fgrep Default fixed string mode

A.fgrep default does not support Bre, ERE, only support fixed string mode, faster than Grep,egrep

B.fgrep command followed by-G selection, which means the BRE is supported

2. sed regular expression characteristics

1) SED supports BRE, ERE

a. The BRE is supported by default

The b.sed instruction is followed by the-R parameter, which indicates the use of Ere

C.sed is used to process text files, to curd the contents of a text file, to process it by line

3. awk Regular Expression features

1) awk (gawk) Regular expression features

A. Default support ere

B. As long as the columns of the text file are processed

Iv. Basic use of the grep command

1, the use of grep syntax

1) grep [option] pattern [FILE ...]

2. grep Common options

1)-C: The number of output matching rows

2)-I: do not distinguish between lowercase

3)-R: Recursive lookup (current search directory and its subdirectories)

4)-N: Output matching line and line number

5)-L: Only the file name of the matching string is output when multiple files are found

6)-V: Reverse match, which shows unmatched rows

7)-O: Show only the string that matches to itself

8)-Q: Equivalent to--quiet, silent mode ( can be used to determine whether this search contains this string, suitable for use in the script )

Notice: If there are metacharacters in the regular expression, use quotation marks to expand them, or use double quotes for variables in the pattern, or single quotes.

A.CENTOS7: Using grep aliases, the matching results are highlighted by default

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/7D/3F/wKiom1bjjmuhK-UYAAAZR82wQ00686.png "title=" image 1. PNG "alt=" Wkiom1bjjmuhk-uyaaazr82wq00686.png "/>

B.CENTOS6: Default is not used

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/7D/3F/wKiom1bjjqihuWTBAAAWnz1L_ts954.png "title=" Image 2. PNG "alt=" Wkiom1bjjqihuwtbaaawnz1l_ts954.png "/>

3, based on the use of basic regular expression meta-characters

1) Character matching

A.. Match any single character

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/7D/40/wKioL1bjz1Oy60HHAAANK2VlADA509.png "title=" Image 4. PNG "alt=" Wkiol1bjz1oy60hhaaank2vlada509.png "/>

B. [] matches a single character within a specified range

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/7D/42/wKiom1bj0Fuz4ip3AAAMZHYNzGE977.png "title=" Image 4. PNG "alt=" Wkiom1bj0fuz4ip3aaamzhynzge977.png "/>

c. [^] characters outside the matching range

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/7D/41/wKioL1bj1lHiSi2vAAATeRI2caE099.png "title=" image 10. PNG "alt=" Wkiol1bj1lhisi2vaaateri2cae099.png "/>

2) Number of matches

A. * match the preceding character any time (0,1 or multiple)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/7D/42/wKiom1bj2cjQp52lAAAJyJ2ubqQ249.png "title=" image 11. PNG "alt=" Wkiom1bj2cjqp52laaajyj2ubqq249.png "/>

B. * Any character at any length after a

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/7D/41/wKioL1bj2wmzUzg7AAAQx_cPNa0563.png "title=" image 13. PNG "alt=" Wkiol1bj2wmzuzg7aaaqx_cpna0563.png "/>

C. \+ matches the preceding character at least once

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/7D/43/wKiom1bj21-izYnSAAAJVlwx0SU046.png "title=" Image 3. PNG "alt=" Wkiom1bj21-izynsaaajvlwx0su046.png "/>

D. \? Matches the preceding character 0 or 1 times

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/7D/43/wKiom1bj2-GQm-UYAAAJt3I9dvQ911.png "title=" Image 4. PNG "alt=" Wkiom1bj2-gqm-uyaaajt3i9dvq911.png "/>

E. \{m\} The characters preceding it appear m times and M is a nonnegative integer

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7D/43/wKiom1bj3K7ibxkbAAALsutoKQ0988.png "title=" image 5. PNG "alt=" Wkiom1bj3k7ibxkbaaalsutokq0988.png "/>

F. \{m,n\} The characters preceding it appear m times and M is a nonnegative integer

\{0,n\} up to n times

\{m,\} at least m times

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/7D/41/wKioL1bj3iGQ6Vc1AAAOc34cCfE696.png "title=" image 6. PNG "alt=" Wkiol1bj3igq6vc1aaaoc34ccfe696.png "/>

3) Position anchoring

Limit the text that the pattern matches to, only where it is in the target text

A. ^ The beginning of the line anchoring, used for the leftmost mode, ^pattern

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/7D/43/wKiom1bj3wHDvKaaAAAOl4kZNJs042.png "title=" Image 7. PNG "alt=" Wkiom1bj3whdvkaaaaaol4kznjs042.png "/>

B. $ line-tail anchoring, for the right-most mode, $pattern

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/7D/41/wKioL1bj4FnzEIV7AAAP1B_wJ1s788.png "title=" Image 8. PNG "alt=" Wkiol1bj4fnzeiv7aaap1b_wj1s788.png "/>

C. ^pattern$ matches entire row

^$ to match empty rows

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/7D/43/wKiom1bj4KaiU-I9AAARuPo2U8U996.png "title=" Image 9. PNG "alt=" Wkiom1bj4kaiu-i9aaarupo2u8u996.png "/>

4) Word anchoring

A. \< or \b The first anchor, used for the left side of the word pattern, in the form of \<pattern,pattern\b

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/7D/41/wKioL1bj4mSyJvTnAAAO8DJnBYs336.png "title=" image 10. PNG "alt=" Wkiol1bj4msyjvtnaaao8djnbys336.png "/>

B. \> or \b Ending anchoring, for the right side of the word pattern, formatted as pattern\>,pattern\b

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/7D/43/wKiom1bj4lTBttrnAAANVKlQ0T4892.png "title=" image 12. PNG "alt=" Wkiom1bj4ltbttrnaaanvklq0t4892.png "/>

5) Grouping and referencing

A. \ (pattern\) handles this matching string as an integral whole

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/7D/41/wKioL1bj5aHw_OcqAAAQWpDz-Rw957.png "title=" image 13. PNG "alt=" Wkiol1bj5ahw_ocqaaaqwpdz-rw957.png "/>

6) | Or

A. a|b represents a or B, which represents the left or right side of the entire expression

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/7D/43/wKiom1bj5kLQjDL_AAAJ57KQ73k824.png "title=" image 14. PNG "alt=" Wkiom1bj5klqjdl_aaaj57kq73k824.png "/>

Five, the expansion of egrep expression


This article is from the "11304510" blog, please be sure to keep this source http://11314510.blog.51cto.com/11304510/1750400

The grep of the Linux text processing weapon

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.