In the Linux system, there are three commonly used tools for processing text, and grep is one of the Three Musketeers, commonly known as text processing.
grep is a powerful text search tool whose full name is: Globally search a Regular Expression and Print. It works by searching and filtering the text based on the given "pattern" of the regular expression engine and printing the matching lines. Therefore, to understand the use of grep we can proceed from two aspects, the use of the 1:grep command, 2: regular expression.
How to use the 1,grep command
First we can use the man command to see the use of grep for help.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7C/FE/wKiom1beM6XQQjiMAAAjFgDi6aA488.png "title=" Mangrep.png "alt=" Wkiom1bem6xqqjimaaajfgdi6aa488.png "/>
The first form of use is: grep [OPTIONS] PATTERN [FILE ...]
Examples of Use:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7C/FF/wKiom1beN4Lj4UVoAAAliuJaCNk334.png "title=" Grep1.png "alt=" Wkiom1ben4lj4uvoaaaliujacnk334.png "/>650" this.width=650; "src=" http://s3.51cto.com/wyfs02/M01 /7c/ff/wkiom1ben7nykvnzaaax_v_asng278.png "title=" Grep2.png "alt=" Wkiom1ben7nykvnzaaax_v_asng278.png "/>
It can be seen that the target text is matched by line according to "Root (Filter)", and the qualifying line is printed out.
Common options--color=auto: Color The text to match to highlight;
Examples of Use:
Because the alias grep= "grep--color=auto" (which can be viewed through the type command) is already defined in the shell's environment variable configuration file in CentOS7, we perform the undo alias Operation First.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7C/FD/wKioL1beOrXi1OzlAAAuOw6eeeI917.png "title=" Grep3.png "alt=" Wkiol1beorxi1ozlaaauow6eeei917.png "/>
See that this way of showing results is more intuitive, so the grep command we use in the following example is an alias that has already defined the Grep=grep--color=auto.
Common Options-I: ignore character case;
Examples of Use:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7C/FF/wKiom1beO6uBX7zoAAAs-HMFdi0661.png "title=" Grep4.png "alt=" Wkiom1beo6ubx7zoaaas-hmfdi0661.png "/>
Common Options-o: Displays only the text that matches to itself;
Examples of Use:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7C/FE/wKioL1bePO-ASf3GAAAhsLR9D1U608.png "title=" Grep5.png "alt=" Wkiol1bepo-asf3gaaahslr9d1u608.png "/>
Common Option-V,--invert-match: reverse matching;
Examples of Use:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7C/FE/wKioL1bePX-ToBWzAABQV-P4qtQ135.png "title=" Grep6.png "alt=" Wkiol1bepx-tobwzaabqv-p4qtq135.png "/>
Common Options-Q,--quiet,--silient: Silent mode, do not output any information;
Examples of Use:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7C/FE/wKioL1bePlHiH9p1AAAhIM-4y5M402.png "title=" Grep7.png "alt=" Wkiol1beplhih9p1aaahim-4y5m402.png "/>
Common Options-A NUM,--after-context=num: simultaneously displays the line of text matched to and its post NUM line
Examples of Use:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7C/FE/wKioL1beQJTxiI1vAAAzE4TcERk551.png "title=" Grep8.png "alt=" Wkiol1beqjtxii1vaaaze4tcerk551.png "/>
Common option-B num,--before-context=num: Displays both the matched text line and its previous NUM line
Examples of Use:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7C/FF/wKiom1beQMijO0uXAAAsgSQlqOg254.png "title=" Grep10.png "alt=" Wkiom1beqmijo0uxaaasgsqlqog254.png "/>
Note: The line that is matched to does not show the first two lines of it at the same time because it is already the line of text.
Common options-C num,-num,--context=num: Simultaneous display of matched lines of text and its front and rear NUM lines
Examples of Use:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/00/wKiom1beQsOR9ByjAAA6Hfw3Z7k351.png "title=" Grep11.png "alt=" Wkiom1beqsor9byjaaa6hfw3z7k351.png "/>
The second use of the format grep [OPTIONS] [-E PATTERN |-f FILE] [FILE ...]
There are two ways of using this format
The first uses the-E option:-E pattern,--regexp=pattern: multi-mode mechanism;
Examples of Use:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7C/FE/wKioL1beRcbxqPlvAABMqepH_Lk386.png "title=" Grep12.png "alt=" Wkiol1bercbxqplvaabmqeph_lk386.png "/>
The second use of the-F option: F file,--file=file:file for each line contains a "pattern" text file, that is, grep script;
Examples of Use:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/00/wKiom1beR3agPVBiAAAPJs83F_w739.png "title=" Grep14.png "alt=" Wkiom1ber3agpvbiaaapjs83f_w739.png "/>650" this.width=650; "src=" http://s5.51cto.com/wyfs02/ M00/7d/00/wkiom1ber5qtxiaiaaayyla8b5o042.png "title=" Grep13.png "alt=" Wkiom1ber5qtxiaiaaayyla8b5o042.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/00/wKiom1beR6_hbbvTAAA83j_3ekk795.png "title=" Grep15.png "alt=" Wkiom1ber6_hbbvtaaa83j_3ekk795.png "/>
Part two: Using regular expressions
Regular Expressions: Regular expression, REGEX
A pattern written by a class of special characters and text characters, some of which do not represent their literal meaning, but are used to denote the function of control or distribution;
Regular expressions can be divided into two categories:
1, basic Regular expression: BRE (basic Regular expression)
1) meta-characters:
.: means matching any single character
Examples of Use:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/01/wKiom1beUwrz8JewAAAgm_41M2o405.png "title=" Grep16.png "alt=" Wkiom1beuwrz8jewaaagm_41m2o405.png "/>
[]: matches any single character within the range
[^]; match any single character outside the range (inverse)
Examples of Use:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/01/wKiom1beVdGzfal-AAAnonTQAhg958.png "title=" Grep17.png "alt=" Wkiom1bevdgzfal-aaanontqahg958.png "/>
Commonly used ranges denote intervals:
[[:d Igit:]]: denotes all numbers [0-9]
[[: Lower:]]: denotes all lowercase letters, equivalent to [a-z]
[[: Upper:]]: denotes all uppercase letters, equivalent to [a-z]
[[: Alpha:]]: denotes all letters, equivalent to [a-za-z]
[[: Alnum:]]: denotes all letters and numbers, equivalent to [a-za-z0-9]
[[: Graph:]]: denotes all non-null characters (without spaces and control characters)
[[: Cntrl:]]: denotes all control characters
[[:p UNCT:]]: denotes all punctuation marks
[[:p Rint:]]: denotes all non-null characters (including spaces)
[[: Space:]]: denotes whitespace characters
Details can be viewed using the man 7 glob.
Examples of Use:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/01/wKiom1beWzrjx-wgAABIGqvwpC0448.png "title=" Grep18.png "alt=" Wkiom1bewzrjx-wgaabigqvwpc0448.png "/>
2) Number of matches: used to specify the number of occurrences of the character after the function is to limit the number of characters in front of its occurrence, the default work in greedy mode;
*: matches the preceding character any number of times (0,1 or more), then. * Can be expressed as any character at any time, that is, any character of any length.
\+: Matches the preceding character at least once
\?: matches the preceding 0 or 1 times, that is, the preceding character is optional
\{m\}: Matches the preceding character m times, M is a nonnegative integer
\{m,n\}: Matches the preceding character at least m times, up to N times
\{0,n\}: Matches up to its preceding character n times
\{m,\}: Matches at least the preceding characters m times
Using the example
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/01/wKiom1beYAuhfjycAABcokvLy5s217.png "title=" Grep19.png "alt=" Wkiom1beyauhfjycaabcokvly5s217.png "/>
3) Position anchoring: The text that the restricted mode matches to can only appear in the specific position of the target text;
^: Anchor at the beginning of the line; for the leftmost side of the pattern, ^pattern
$: End of line anchoring; for the right side of the pattern, pattern$
^pattern$: To make PATTERN exactly match a whole line;
^$: Empty line;
^[[:space:]]*$: Blank lines or lines with only white space characters
Using the example
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/00/wKioL1beY4Gjm6NiAAA1iWhv7UM593.png "title=" Grep20.png "alt=" Wkiol1bey4gjm6niaaa1iwhv7um593.png "/>
\< or \b: The initial anchor for the left side of the word pattern, formatted as \<pattern, \bpattern
\> or \b: The ending anchor for the right side of the word pattern, formatted as PATTERN\> pattern\b
\<pattern\>: Word anchoring;
Word: A continuous character consisting of non-special characters (a string) is called a word;
Examples of Use:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/00/wKioL1beZg7yupLDAAA6IKiiU34684.png "title=" Grep21.png "alt=" Wkiol1bezg7yupldaaa6ikiiu34684.png "/>
4) grouping and referencing:
\ (pattern\): The character matching this PATTERN is treated as a non-infringing whole;
Note: The patterns in the grouping brackets match the characters that are automatically recorded in the internal variables by the regular expression engine, which 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;
\2: The string that the pattern in the second set of parentheses matches to;
......
Back reference: Refers to the string that matches the pattern in the preceding parentheses;
Using the example
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/03/wKiom1bebpPQhErvAAArUsQlRlU247.png "title=" Grep22.png "alt=" Wkiom1bebppqhervaaarusqlrlu247.png "/>
Common options:
-E,--extended-regexp: supports the use of extended regular expressions;
-F,--fixed-strings: Supports the use of fixed strings and does not support regular expressions;
-G,--basic-regexp: Supports the use of basic regular expressions;
-P,--perl-regexp: supports the use of pcre regular expressions;
2, extended Regular expression: ERE (Extend Regular Express)
Is the same as the basic regular expression:
Metacharacters:. ; * ; [ ]; [^]
Location anchoring:
^: Anchor at the beginning of the line;
$: End of line anchoring;
differs from the basic regular expression:
Number of matches:
+: Matches the preceding character at least once, equal to the \+ in the basic regular expression
?: Matches the previous 0 or 1 times, equivalent to the basic regular expression in the \?
{m}: matches the preceding character m times, equivalent to \{m\} in the basic regular expression
{M,n}: matches the preceding character at least m times, equivalent to \{m,n\} in the base regular expression
{0,n}: matches up to its preceding character n times, equivalent to \{0,n\} in the base regular expression
{m,}: matches at least the preceding character m times, equivalent to \{m,\} in the basic regular expression
Grouping and referencing:
(pattern): equivalent to the basic regular expression in \ (pattern\), the reference method and the basic regular expression are the same, all for the back reference: \1, \2, ...
Features that increase compared to basic regular expressions
| : OR
A|b: Indicates a or b
C|cat: Indicates C or cat
(C|C) at: Indicates Cat or cat
Using the example
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7D/04/wKiom1bedyriKT60AAArB_vE_Ds116.png "title=" Grep23.png "alt=" Wkiom1bedyrikt60aaarb_ve_ds116.png "/>
Egrep: Support for grep commands using extended regular expressions, equivalent to GREP-E;
Fgrep: Support for grep commands using extended regular expressions, equivalent to grep-f;
grep, Egrep, and corresponding regular expressions and usages in Linux