First, preface
grep is a powerful text processing tool, full name: Global search Regular expression and print out the Line,grep line uses regular expression to match text. The entire line of matching text is then displayed (unless you use a specific option, such as GREP-V).
Second, grep usage
grep command usage:
grep [OPTIONS] PATTERN [FILE ...]
grep [OPTIONS] [-E PATTERN |-f file] [FILE ...]
To use grep usage, we have to learn the following simple expressions
2.1. Usage of regular expressions
First, we need to understand what a regular expression is. The so-called regular expression is the use of a single string to describe and match a series of strings that conform to a certain grammatical rule. Regular expressions are made up of some common character and metacharacters characters. Ordinary characters include uppercase and lowercase letters and numbers, while metacharacters have special meanings.
Meta-character usage for 2.1.1 and basic regular expressions
A: Character matching
| . |
Match any single character |
For example: grep ' roo. '/etc/passwd |
| [] |
Matches any single character within the specified range |
For example: grep ' [ro] '/etc/passwd |
| [^] |
Take counter |
For example: grep ' [^ro] '/etc/passwd |
B: Number of matches
| * |
Match any number of times |
For example: grep ' roo* '/etc/passwd |
| \? |
Matches the preceding character appears 0 or 1 times |
For example: grep ' roo\? '/etc/passwd |
| \{m\} |
Matches its preceding character appears m times |
For example: grep ' ro\{2\} '/etc/passwd |
| \{m,\} |
Match its preceding characters to appear at least m times |
For example: grep ' ro\{2,\} '/etc/passwd |
| \{m,n\} |
Match its preceding characters to appear at least m times, up to N times |
For example: grep ' ro\{2,3\} '/etc/passwd |
| \{0,n\} |
Matches its preceding character up to n times |
For example: grep ' ro\{0,2\} '/etc/passwd |
C: Position anchor match
| ^ |
Beginning of line anchor character |
For example: grep ' ^root '/etc/passwd |
| $ |
End of line anchor character |
For example: grep ' shell$ '/etc/passwd |
| \< |
The first anchor character of the word |
For example: grep ' \<root '/etc/passwd |
| \> |
Ending anchor |
For example: grep ' shell\> '/etc/passwd |
D: Grouping
| \(\) |
Grouping, the contents of a pattern match in a group can be referenced |
For example: grep ' \ (root\). *\1 '/etc/passwd |
E: Reference
| \# |
Reference Group # of content (#为数字) |
For example: grep ' \ (root\). *\1 '/etc/passwd |
2.1.2, extended regular expression meta-character usage
A: Character matching
| . |
Match any single character |
For example: Egrep ' roo. '/etc/passwd |
| [] |
Matches any single character within the specified range |
Example: Egrep ' [ro] '/etc/passwd |
| [^] |
Take counter |
Example: Egrep ' [^ro] '/etc/passwd |
B: Number of matches
| * |
Match any number of times |
Example: Egrep ' roo* '/etc/passwd |
| + |
Match preceding character appears more than 1 times |
Example: Egrep ' root '/etc/passwd |
| ? |
Matches the preceding character appears 0 or 1 times |
For example: Egrep ' roo? '/etc/passwd |
| {m} |
Matches its preceding character appears m times |
Example: Egrep ' ro{2} '/etc/passwd |
| {m,} |
Match its preceding characters to appear at least m times |
For example: Egrep ' ro{2,} '/etc/passwd |
| {M,n} |
Match its preceding characters to appear at least m times, up to N times |
Example: Egrep ' ro{2,3} '/etc/passwd |
| {0,n} |
Matches its preceding character up to n times |
Example: Egrep ' ro{0,2} '/etc/passwd |
C: Position anchor match
| ^ |
Beginning of line anchor character |
Example: Egrep ' ^root '/etc/passwd |
| $ |
End of line anchor character |
Example: Egrep ' shell$ '/etc/passwd |
| \< |
The first anchor character of the word |
Example: Egrep ' \<root '/etc/passwd |
| \> |
Ending anchor |
Example: Egrep ' shell\> '/etc/passwd |
D: Grouping
| ( ) |
Grouping, the contents of a pattern match in a group can be referenced |
For example: Egrep ' (root\). *\1 '/etc/passwd |
E: Or
| | |
Or |
Example: Egrep ' Bash|nologin '/etc/passwd |
F: Reference
| \number |
Reference the first few matches |
For example grep ' (a). *\1 '/etc/passwd |
2.3 grep Common options
| -V |
Reverse Selection |
Example: Grep-v ' root '/etc/passwd |
| -O |
Only the matching string itself is displayed, not the row |
Example: Grep-o ' root '/etc/passwd |
| -I. |
Ignore case |
Example: Grep-i ' root '/etc/passwd |
| -E |
Supports the use of an extended regular expression |
For example: Grep-e ' (root). *\1 '/etc/passwd |
| -A |
Back n rows |
Example: Grep-a 3 ' root '/etc/passwd |
| -B |
Front n rows |
Example: Grep-b 3 ' mysql '/etc/passwd |
| -C |
n rows before and after each |
Example: Grep-c 3 ' mysql '/etc/passwd |
Third, examples
3.1. Display the lines in the/etc/passwd file that end with bash
grep ' \<bash\>$ '/etc/passwd root:x:0:0:root:/root:/bin/bash leon:x:500:4400::/home/leon:/bin/bash Mandirva : X:2200:4400::/home/mandirva:/bin/bash ... centos:x:2241:2241::/home/centos:/bin/bash user1:x:2242:2242::/home /user1:/bin/bash
3.2, displays the two-digit or three-digit number in the/etc/passwd file
grep ' \<[[:d igit:]]\{2,3\}\> ' /etc/passwd mail:x:8:12:mail:/var/spool/ mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/ Sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp : X:14:50:ftp user:/var/ftp:/sbin/nologin nobody:x:99:99:nobody:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin saslauth:x:499:76: "Saslauthd user":/var/empty/saslauth:/sbin/nologin &NBSP;POSTFIX:X:89:89::/VAR/SPOOL/POSTFIX:/SBIN/NOLOGIN&NBSP;&NBSP;&NBSP;&NBSP;SSHD:X:74:74: Privilege-separated ssh:/var/empty/sshd:/sbin/nologin leon:x:500:4400::/home/leon :/bin/bash openstack:x:501:501:this ' S openstack:/home/openstack:/bin/bash
3.3, display the ' netstat-tan ' command result with ' LISTEN ' followed by 0, one or more whitespace characters end of the line
netstat -tan | grep ' LISTEN ' [[:space:]]* tcp 0 0 0.0.0.0:22 0.0.0.0:* listen tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 : :1:25 :::* listen
3.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
grep ' ^\ ([[: Alnum:]]*\):. *\1$ '/etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/ Shutdown Halt:x:7:0:halt:/sbin:/sbin/halt Nologin:x:2208:2208::/home/nologin:/sbin/nologin
3.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)
Grep-e ' ^root|^centos|^user1 '/etc/passwd | cut-d:-f1,3,7 root:0:/bin/bash centos:2241:/bin/bash User1:2242:/bin/bash
3.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
Grep-e ' \<[[:alpha:]]+_?\ (\) '/etc/rc.d/init.d/functions checkpid () {daemon () {Killproc () {PIDFILEOFPR OC () {Pidofproc () {status () {success () {failure ()} {passed () {warning () {action () {strstr ()} {Confirm () {
3.7, use echo to output a path, and then egrep find its path base name; Further use Egrep to remove its directory name
echo/etc/passwd | Egrep-o ' [[: alnum:]]+$ ' passwdecho/etc/passwd | Egrep-o ' ^ (/). *\1 '/etc/
3.8. Find the number between 1-255 in the result of ifconfig command execution
Ifconfig | Grep-eo ' \< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> ' | Sort | Uniq 1 2 6 8 27 41 56 64 75 101 106 127 128 168 192 255
This article is from the "leon91" blog, make sure to keep this source http://leon91.blog.51cto.com/3639216/1690755
grep Regular Expression