Grep
Function: Matches the specified row by a specified pattern
Common options:
-E extended regular expression matches the equivalent of the EGREP command
-O get only what matches to
-V matches the opposite of the regular expression
--color=auto to highlight the contents of the search
-A num matches the row to the search and the NUM line below the line
-B num matches the row to the search and the num row above the line
-C num matches the row to the search and the top and bottom num rows
-N Displays the number of rows in the file
-R Recursive directory search for specified content files
-I regular expression content is case insensitive
The basic regular Expression description:
Character Matching:
.: matches any single character;
[]: matches any single character within the specified range;
[^]: matches any single character within the specified range;
[: Lower:] Match lowercase letters
[: Upper:] matches uppercase letters
[: Alpha] matches all letters
[:d Igit:] Match all numbers
[: Alnum:] matches all numeric letters [: Alpha:] and [: Alpha:] and two for one
[: space] represents a space character such as tab, line break, space, etc.
[:p UNCT:] represents punctuation marks such as:'! "# $% & ' () * +,-. / : ; < = >? @ [] ^ _ ' {|}
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, [M,n]
\{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 and so on.
Extend the metacharacters of regular expressions:
Character Matching:
Same as the basic regular expression
Number of Matches
*: 0 or more times
?: 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: 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;
Group:
Same as the basic regular expression
The concept of support or
A|b: Match A or b
Practice:
1 Displays the lines in the/etc/passwd file that end with bash
Answer: grep--color=auto ' bash$ '/etc/passwd
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/3F/wKioL1XfP0HQ33lmAACyuAD8X4Q975.jpg "style=" float: none; "title=" Grep1.png "alt=" Wkiol1xfp0hq33lmaacyuad8x4q975.jpg "/>
2 display two-digit or three-digit numbers in a/etc/passwd file
Answer: A. Basic regular expression:grep--color=auto ' \b[[:d igit:]]\{2,3\}\b '/etc/passwd
B. Extended regular expression:grep--color=auto-e ' \b[[:d igit:]]{2,3}\b '/etc/passwd
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/72/42/wKiom1XfPSjhntOUAAQ0baHs9LU059.jpg "title=" Grep2.png "style=" Float:none; "alt=" wkiom1xfpsjhntouaaq0bahs9lu059.jpg "/>
Because the basic regular expressions are similar to the way they are extended, the following solutions take the form of extending regular expressions
3 Displays the line with ' LISTEN ' followed by 0, one or more white-space characters in the ' netstat-tan ' command result;
Answer: Netstat-tan | grep--color=auto-e ' listen[[:space:]]*$ '
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/72/3F/wKioL1XfP0Kx-ZdrAADFhJdeDak723.jpg "title=" Grep3.png "style=" Float:none; "alt=" wkiol1xfp0kx-zdraadfhjdedak723.jpg "/>
4 Add user Bash,testbash, basher and Nologin user (Nologin user's shell is/sbin/nologin), and then find the line of user name and shell name in/etc/passwd file;
Answer: 1.useradd bash
2.useradd Testbash
3.useradd basher
4.useradd-s/sbin/nologin Nologin
5.grep--color=auto-e ' ^ ([[: alnum:]]+):. *\1$ '/etc/passwd
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/72/3F/wKioL1XfP0Ki_iaqAADvS6R_cS0611.jpg "title=" Grep4.png "style=" Float:none; "alt=" wkiol1xfp0ki_iaqaadvs6r_cs0611.jpg "/>
5 Displays the default shell and UID for root, CentOS, or User1 users on the current system;
Answer: egrep ' ^\<root|centos|user1\> '/etc/passwd | cut-d:-f3,7
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/72/42/wKiom1XfPSqz6lI4AACQQXuSyc0904.jpg "title=" Grep5.png "style=" Float:none; "alt=" wkiom1xfpsqz6li4aacqqxusyc0904.jpg "/>
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;
Answer: grep-e--color=auto ' \<[0-9a-za-z_]+\>\ (\) '/etc/rc.d/init.d/functions
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/72/3F/wKioL1XfP0OgsLIrAAH7aR7gbrw569.jpg "title=" Grep6.png "style=" Float:none; "alt=" wkiol1xfp0ogsliraah7ar7gbrw569.jpg "/>
7 use Echo to output a path, and then egrep to find its path base name; further: Use Egrep to remove its directory name;
Answer:
Path Base name command: echo/usr/local/nginx/conf/nginx.conf | egrep-o ' ([^/]*) $ '
Path Directory name command: echo/usr/local/nginx/conf/nginx.conf | Egrep-o ' ^ (. *)/'
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/72/42/wKiom1XfPSqD0AjGAAEKtixp2E4168.jpg "title=" Grep7.png "style=" Float:none; "alt=" wkiom1xfpsqd0ajgaaektixp2e4168.jpg "/>
8 Find the number between 1-255 in the result of ifconfig command execution;
Answer: ifconfig | egrep--color=auto ' \< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> '
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/72/43/wKiom1XfPdrzLbHZAAGdGV_8_uQ815.jpg "title=" Grep8.png "alt=" Wkiom1xfpdrzlbhzaagdgv_8_uq815.jpg "/>
Linux grep usage and grep usage exercises