Linux Text Filter Finder Common regular expressions and usages of grep and Egrep

Source: Internet
Author: User
Tags expression engine egrep

The corresponding regular expressions and usages of grep and egrep in the grep family.

The 1.grep family is one of the three great musketeers of text processing. (Grep,sed,awk)

grep: (Global search REgular expression and Print out of the line). It supports the use of basic regular expressions.

Egrep: supports the use of extended regular expressions

Fgrep: using regular expressions is not supported (typically used in large Web site log searches)

The role of grep: filter to find keywords and print matching lines.

grep usage: grep [options] Match pattern [file] ....

2. Regular Expressions: Reguiar expression, referred to as the Regex.

A single string that describes or matches a series of strings that conform to a certain syntactic rule.

In many text editors or other tools, regular expressions are often used to retrieve or replace text content that conforms to a pattern.

Regular expressions are divided into two categories: the basic Regular expression: BRE: Extended Regular expression: ERE

Several common options for 3.grep:

--color=auto: Display color, (default Centos7 already set)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/47/wKiom1bkNh_zQpBoAAAL9jK3kYw200.png "title=" 0.1. PNG "alt=" Wkiom1bknh_zqpboaaal9jk3kyw200.png "/>

-N: Show line numbers

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/46/wKioL1bkNtnAENV2AAAMs4xrSNk258.png "title=" 0.2. PNG "alt=" Wkiol1bkntnaenv2aaams4xrsnk258.png "/>

-C: Count the number of rows that meet the requirements

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/46/wKioL1bkN23jWHJSAAAI9nZKJgk225.png "title=" 0.3. PNG "alt=" Wkiol1bkn23jwhjsaaai9nzkjgk225.png "/>

-V: Inverse, not containing the selected character

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7D/46/wKioL1bkN6fyxjVEAAAV9yVw-rI999.png "title=" 0.4. PNG "alt=" Wkiol1bkn6fyxjveaaav9yvw-ri999.png "/>

-I: Case insensitive


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/48/wKiom1bkN4DTsD2FAAASGzJ1z6M235.png "style=" float: none; "title=" 0.5.png "alt=" Wkiom1bkn4dtsd2faaasgzj1z6m235.png "/>

-W: Match word

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/46/wKioL1bkOAmiWAYIAAAXEjeeZ9k982.png "style=" float: none; "title=" 0.6.png "alt=" Wkiol1bkoamiwayiaaaxejeez9k982.png "/>

-Q: Silent mode, does not output any information

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7D/48/wKiom1bkN4OCQ4qlAAAXll-eAoE397.png "style=" float: none; "title=" 0.7.png "alt=" Wkiom1bkn4ocq4qlaaaxll-eaoe397.png "/>

-O: Show only the text that matches to itself

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/48/wKiom1bkN4PCzkKFAAAL26n5HVo361.png "style=" float: none; "title=" 0.8.png "alt=" Wkiom1bkn4pczkkfaaal26n5hvo361.png "/>

4. Character matching:

.: matches any single character;

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/46/wKioL1bkOAuiApa3AAA9u-J5ptc911.png "style=" float: none; "title=" 0.9.png "alt=" Wkiol1bkoauiapa3aaa9u-j5ptc911.png "/>

[]: matches any single character within the range;

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/46/wKioL1bkOAyRttsQAAA1IhkMObw618.png "style=" float: none; "title=" 1.0.png "alt=" Wkiol1bkoayrttsqaaa1ihkmobw618.png "/>

[^]: Any single character in a non-matching range:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/48/wKiom1bkN4WCdHvsAAAYeV4fAmY788.png "style=" float: none; "title=" 1.1.png "alt=" Wkiom1bkn4wcdhvsaaayev4famy788.png "/>

5. Match words: Use the number of times that you want to specify the number of occurrences of the character that precedes it, and the default work in greedy mode

*: matches the preceding character any time (including 0 times) the horizontal bar in the picture indicates that it appears 0 times

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/48/wKiom1bkN4eyhSfHAAA5xBWvZWU097.png "style=" float: none; "title=" 1.2.png "alt=" Wkiom1bkn4eyhsfhaaa5xbwvzwu097.png "/>

\+: Matches the preceding character at least 1 times: compared to the previous picture, there is no line of C, and no match is reached.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/46/wKioL1bkOA_iAH9oAAA8RHB8Eeo054.png "style=" float: none; "title=" 1.3.png "alt=" Wkiol1bkoa_iah9oaaa8rhb8eeo054.png "/>

. *: Matches any character of any length

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7D/48/wKiom1bkN4nDFQTHAAA8UAb93eM917.png "style=" float: none; "title=" 1.4.png "alt=" Wkiom1bkn4ndfqthaaa8uab93em917.png "/>

\? : Matches the previous 0 or 1 ideas: for example, in the match "x+?y" mode, "XXXYACN" can also be matched to.

It is "XY" from 1 units of Y forward. Because? 0 to 1, please don't confuse.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/48/wKiom1bkOgLhw0DfAAA7q75tXs8214.png "style=" float: none; "title=" 1.5.png "alt=" Wkiom1bkoglhw0dfaaa7q75txs8214.png "/>

\{m\}: The preceding character appears m times, m non-negative

By the picture we can see that when we select "C\{2\}" The C appears at least 2 times because there is no cc so we have nothing to match, but when we change C to R we can match a lot.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/46/wKioL1bkOouARSMOAAAfrd_tQrY981.png "style=" float: none; "title=" 1.6.png "alt=" Wkiol1bkoouarsmoaaafrd_tqry981.png "/>

\{m,n\}: The preceding character appears at least m times at most n times, such as r\{2,3\}. Indicates that R appears at least 2 times and occurs up to 3 times


6. Position limit: The text that restricts the pattern to match can only appear in the location of the target text;

^: Beginning of line: ^pattern

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/48/wKiom1bkPtaj0EgxAAAO-72V0Fk040.png "title=" complement. png " alt= "Wkiom1bkptaj0egxaaao-72v0fk040.png"/>

$: Line End limit: patern$

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/48/wKiom1bkOgWgbqksAAAQsk4U6RI757.png "style=" float: none; "title=" 1.8.png "alt=" Wkiom1bkogwgbqksaaaqsk4u6ri757.png "/>

^patern$: Blank line. Represents Patern itself as a complete line

Because there is no B in/etc/passwd. H is a whole line, so there is no match.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7D/46/wKioL1bkOozB5cgTAAAHyJ3ZsGY292.png "style=" float: none; "title=" 1.9.png "alt=" Wkiol1bkoozb5cgtaaahyj3zsgy292.png "/>

"^$": Blank line

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7D/46/wKioL1bkOo3Qs1GYAAA0xSPTvKc351.png "style=" float: none; "title=" 2.0.png "alt=" Wkiol1bkoo3qs1gyaaa0xsptvkc351.png "/>

Word: A continuous character consisting of non-special characters, including a string, is called a word.

\< or \b: The initial anchor for the left side of the word pattern, formatted as \<pattern, \bpattern

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/48/wKiom1bkOgaTiEMaAAAVNN2J8JY772.png "style=" float: none; "title=" 2.1.png "alt=" Wkiom1bkogatiemaaaavnn2j8jy772.png "/>

\> or \b: The ending anchor for the right side of the word pattern, formatted as PATTERN\> pattern\b

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/46/wKioL1bkOo6yTv-iAAAh6ETKazE662.png "style=" float: none; "title=" 2.2.png "alt=" Wkiol1bkoo6ytv-iaaah6etkaze662.png "/>

\<pattern\>: Word anchoring;

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/48/wKiom1bkOgiQSFVmAAAG6Aoc-Bo893.png "style=" float: none; "title=" 2.3.png "alt=" Wkiom1bkogiqsfvmaaag6aoc-bo893.png "/>

\ (pattern\): The character matching this PATTERN is treated as a non-infringing whole;

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/46/wKioL1bkOpCw45qcAAAMG1tff7o755.png "style=" float: none; "title=" 2.4.png "alt=" Wkiol1bkopcw45qcaaamg1tff7o755.png "/>

7. The characters in the block brackets that match the pattern are automatically recorded in the internal variables by the regular expression engine.

These variables are \1, \2, \3, ...

Pat1\ (pat2\) pat3\ (pat4\ (pat5\) pat6\)

\ n: The string that matches the pattern between the nth opening parenthesis in the pattern and the closing parenthesis that matches it (not the pattern, but the result of the pattern match)

\1: The string that the pattern in the first set of parentheses matches to; (pat2\)

\2: The string that the pattern in the second set of parentheses matches to; (Pat4\ (pat5\) pat6\)

\3: The string that the pattern in the third set of parentheses matches to; (PAT5)

Examples of grouping references:

He love his lover. *\ (L.. e\). *\1R compliant

He like his lover. *\ (L.. e\). *\1R does not meet

He love his liker. *\ (L.. e\). *\1R does not meet

He like his liker. *\ (L.. e\). *\1R compliant

Back reference: References the string to which the pattern in the preceding parentheses matches:

8. Two common options:

-E,--extended-regexp: supports the use of extended regular expressions

-F,--fixed-strings: Supports the use of fixed strings, does not support regular expressions, and is equivalent to Fgrep;

-G,--basic-regexp: Supports the use of basic regular expressions;

-P,--perl-regexp: supports the use of pcre regular expressions

-E. Pattern,--regexp=pattern: multi-mode mechanism;

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/7D/48/wKiom1bkQlfigLIfAAAewrtbopU177.png "title=" 2.5. PNG "alt=" Wkiom1bkqlfiglifaaaewrtbopu177.png "/>

- f file,--file=file:file a text file containing a pattern for each line, the grep script;

-A num,--after-context=num indicates that each row is separated and there are num lines displayed after

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/46/wKioL1bkQwbggPCoAAAfW-LzR2E668.png "title=" 2.6. PNG "alt=" Wkiol1bkqwbggpcoaaafw-lzr2e668.png "/>

-B num,--before-context=num represents the front NUM line

-C num,-num,--context=num indicates before and after each NUM line


9.egrep: The grep command with extended regular expressions is supported, which completes commands that grep can accomplish and cannot accomplish.

Equivalent to GREP-E

Usage: egrep [OPTIONS] PATTERN [FILE ...]


With the same options as grep, this is not a re-introduction


Extend the metacharacters of regular expressions:

Character Matching:

.: Any single character

[]: Any single character within the range

[^]: Any single character outside the range

Number of matches:

*: any time;

?: 0 or 1 times;

+:1 times or times; no need to change

{m}: matches m times; no need to change

{M,n}: At least m times, at most n times; no need to change

Note "\" to go to the signifier

Location anchoring:

^: Beginning of the line

$: End of line

\<, \b: The head of the word

\>, \b: suffix

Grouping and referencing:

(pattern): a grouping in which the pattern in parentheses matches to a character that is recorded in a variable inside the hermetical expression engine;

Back reference: \1, \2, ...

Or:

A|b:a or B

C|cat: Indicates C or cat

(C|C) at: Indicates cat or cat.


I was just contact with Linux operations for less than 2 weeks rookie, before also completely did not learn computer, complete 0 basis.

For the regular expressions and usages of GREP,EGREPD, this is the only way to tease out this. The above errors in the place also ask you to help many teachers to correct me, I am extremely grateful.








Linux Text Filter Finder Common regular expressions and usages of grep and Egrep

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.