grep (abbreviated from globally search a Regular expression and print) is a powerful text-search tool that can use regular expressions to search for text and print matching lines. The grep family includes grep, Egrep, and Fgrep.
grep, Egrep, fgrep-print lines matching a pattern
How to use: grep [OPTIONS] PATTERN [FILE ...]
grep [OPTIONS] [-E PATTERN |-f file] [FILE ...]
Options:
--color=auto: The matching to the string to do highlighting;
-V: The display mode does not match the row;
-I: Ignore character size;
-E: Write using extended regular expressions;
-O: Displays only strings that can be matched to the pattern;
Demonstrate:
The matching string is highlighted;
650) this.width=650; "title=" clip_image002 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image002 "src=" http://s3.51cto.com/wyfs02/M01/72/65/ Wkiom1xilorzogfraabrqqh288o114.jpg "" 557 "height="/>
Inverse, displaying rows that are not matched;
650) this.width=650; "title=" clip_image004 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image004 "src=" http://s3.51cto.com/wyfs02/M02/72/61/ Wkiol1ximkerqwj0aadhg5aogs4281.jpg "" 558 "height="/>
Ignore letter case;
650) this.width=650; "title=" clip_image006 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image006 "src=" http://s3.51cto.com/wyfs02/M00/72/61/ Wkiol1ximkigh8jkaabrrksqrf8785.jpg "" 558 "height=" "/>
Using an extended regular expression
650) this.width=650; "title=" clip_image008 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image008 "src=" http://s3.51cto.com/wyfs02/M01/72/61/ Wkiol1ximkishlzxaaa-s983siu058.jpg "" 558 "height="/>
Show only strings that are matched to the pattern itself
650) this.width=650; "title=" clip_image010 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image010 "src=" http://s3.51cto.com/wyfs02/M02/72/61/ Wkiol1ximkjatlhsaabhv9e8boa971.jpg "558" height= "/>"
The metacharacters of the basic regular expression:
Character Matching:
.: matches any single character;
[]: matches any single character within the specified range;
[^]: matches any single character within the specified range;
Number of occurrences: used to specify the number of times after the character;
*: any time;
\?:0 or 1 times;
\+:1 or multiple times;
\{m\}: Precisely limited to M times;
\{m,n\}: At least m times, up to n times;
\{0,n\}: Up to n times;
\{m,\}: at least m times;
. *: Matches any character of any length;
Location anchoring:
^: Anchor at the beginning of the line, for the leftmost mode;
$: End of line anchoring; for the rightmost side of the pattern;
\< \b: The first anchor of the word; the left side of the pattern used to denote the word;
\> \b: the ending anchor; the right side of the pattern used to represent the word;
^$: blank line;
Group: \ (\)
The contents of the pattern in the grouped parentheses are recorded by the regular expression engine during execution, and the built-in variables are saved: The variables are \1, \2, ...
\1: From the left, the first opening parenthesis, and the matching right parenthesis in the middle of the pattern match to the content;
\2:
Back reference: Use a variable to refer to the character that matches the pattern in the preceding grouping brackets;
An extended regular expression:
The grep family has three commands:
grep: basic Regular expression
-e: extending regular expressions
-F: Regular expressions are not supported
Egrep: Extending Regular expressions
Fgrep: Regular expressions are not supported
Extend the metacharacters of regular expressions:
Character Matching:
.: Any single character
[]:
[^]:
Number of matches:
*: any time;
?: 0 or 1 times;
+: more than 1 times;
{m}: exact match m times;
{M,n}: At least m times, up to n times;
Anchoring:
^: Anchoring the beginning of the line
$: Anchor Line End
\<, \b
\>, \b
Group: ()
Back reference: \1, \2, ...
Or:
A|b
C|cat: does not represent cat or cat, but is C or cat;
Exercises:
1. Display the line in the/etc/passwd file ending with bash;
650) this.width=650; "title=" clip_image012 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image012 "src=" http://s3.51cto.com/wyfs02/M00/72/61/ Wkiol1ximkij-griaac3uchju4w468.jpg "" 557 "height=" 111 "/>
2. Display the two-digit or three-digit number in the/etc/passwd file;
650) this.width=650; "title=" clip_image014 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image014 "src=" http://s3.51cto.com/wyfs02/M01/72/61/ Wkiol1ximkihlpztaaed3e1di2k002.jpg "" 558 "height=" 127 "/>
3, display the ' Netstat-tan ' command results with ' LISTEN ' followed by 0, one or more blank characters end of the line;
650) this.width=650; "title=" clip_image016 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image016 "src=" http://s3.51cto.com/wyfs02/M02/72/61/ Wkiol1ximkiwxqd4aacwuixqm_w411.jpg "" 558 "height=" 151 "/>
4. Add user bash, Testbash, basher, and Nologin user (Nologin user's shell is/sbin/nologin), then find the same line in the/etc/passwd file as the user name and its shell name
650) this.width=650; "title=" clip_image018 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image018 "src=" http://s3.51cto.com/wyfs02/M02/72/65/wKiom1XilouQa2_ Oaaa3l9b7sy8163.jpg "" 558 "height="/>
650) this.width=650; "title=" clip_image020 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image020 "src=" http://s3.51cto.com/wyfs02/M00/72/65/ Wkiom1xilouc5l47aaba1jjmfk0453.jpg "" 557 "height="/>
5. Display the default shell and UID of root, CentOS, or User1 user on the current system (please create these users beforehand, if not present)
650) this.width=650; "title=" clip_image022 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image022 "src=" http://s3.51cto.com/wyfs02/M01/72/65/ Wkiom1xilozriaspaaay4lhyoac109.jpg "" 558 "height="/>
6, find a word in the/etc/rc.d/init.d/functions file (the middle of the word can be underlined) followed by a set of parentheses line
650) this.width=650; "title=" clip_image024 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image024 "src=" http://s3.51cto.com/wyfs02/M02/72/65/ Wkiom1xilozi4dlhaadcd1gz1_o181.jpg "" 558 "height=" 275 "/>
7, use echo to output a path, and then egrep find its path base name; Further use Egrep to remove its directory name
650) this.width=650; "title=" clip_image026 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image026 "src=" http://s3.51cto.com/wyfs02/M00/72/65/ Wkiom1xilozggfctaabs0pio3qc886.jpg "" 558 "height=" "/>
8. Find the number between 1-255 in the result of ifconfig command execution
650) this.width=650; "title=" clip_image028 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image028 "src=" http://s3.51cto.com/wyfs02/M01/72/62/ Wkiol1ximkncpqtsaadtukbzdgm381.jpg "" 558 "height=" 131 "/>
How to use grep commands in Linux