First, the role:
Grep:global serch REgular expression and Print out of the line.
A text search tool that matches the target text line by row according to the user-specified pattern (filter condition);
Second, common options:
grep [OPTIONS] PATTERN [FILE ...]
grep [OPTIONS] [-E PATTERN |-f FILE] [FILE ...]
OPTIONS:
--color=auto: Color The matched text to highlight;
-i:ignorecase, ignoring character case;
-O: Displays only the string that matches to itself;
-V: Reverse display;
-E: Supports the use of extended regular expression metacharacters;
-q,--quiet,--silent: Silent mode, that is, do not output any information;
-A #:after, after # line
-B #:before, front # line
-c #:conext, front and back # lines
Basic regular Expression meta-characters:
Character Matching:
.: matches any single character;
[]: matches any single character within the specified range;
[^]: matches any single character outside the specified range;
Number of matches: used to limit the number of occurrences of the preceding character, after the character to specify the number of occurrences, and the default work is in greedy mode;
*: Matches its preceding character any time, 0, 1, multiple times;
. *: Matches any character of any length;
\?: matches the preceding character 0 or 1 times, that is, the preceding character is optional;
\+: Matches the preceding character 1 or more times, that is, the preceding character must appear at least 1 times;
\{m\}: Matches the preceding character m times;
\{m,n\}: Matches its preceding character at least m times, up to n times;
\{0,n\}: Up to n times;
\{m,\}: at least m times;
Location anchoring:
^: Anchor at the beginning of the line; to the left of the pattern; ^root
$: End of line anchoring; for the rightmost side of the pattern;
^pattern$: Used for PATTERN to match whole line;
^$: blank line;
^[[:space:]]*$: A blank line or a line containing white space characters;
Word: A continuous character (string) consisting of a non-special character is called a word;
\< or \b: The first anchor of the word, used for the left side of the word pattern;
\> or \b: The ending anchor for the right side of the word pattern;
\<pattern\>: matches complete words;
Grouping and referencing:
\ (\): Bind one or more characters together and treat them as a whole;
Note: The contents of the pattern matching in the grouping brackets are automatically recorded in the internal variables by the regular expression engine, and these variables are:
\1: The pattern from the left side, the first opening parenthesis and the matching closing parenthesis, matches the character of the pattern;
\2: The pattern from the left side, the second opening parenthesis, and the matching closing parenthesis to the character;
\3: The pattern from the left side, the third opening parenthesis, and the matching closing parenthesis match the character to the pattern;
...
grep "\ (L.. e\). *\1 "Love.txt behind the \1 can reference \ (l: e\) in the content;
Back reference: References the pattern in the preceding grouping brackets to the character;
grep [OPTIONS] [-E PATTERN |-f FILE] [FILE ...]
Options:-I,-O,-V,-Q,-A,-B,-C
-G: Support for basic regular expressions
Extend the metacharacters of regular expressions:
Character Matching:
Number of matches:
*: Any time, 0,1 or multiple times;
? : 0 or 1 times, the preceding character is optional;
+: Its preceding character at least 1 times;
{m}: its preceding character m times;
{M,n}: At least m times, up to n times;
{0,n}:
{m,}
Location anchoring:
Grouping and referencing:
(): grouping: Pattern matching in parentheses the hand character is recorded hermetical the internal variables of the expression engine;
Back reference: \1,\2,...
Or:
A|b:a or B:
C|cat:c or Cat
(c| C) At:cat or cat
Examples of commands:
Find out the number of blank lines in the/etc/fstab file
~]# grep "^$"/etc/fstab | Wc-l
1
Find the user-rooted line in the/etc/passwd file
~]# grep "^root\>"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Find the line in the/etc/passwd file with the same user name as the shell name
~]# grep--color-e "^ ([^:]+\>). *\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
Find the line that starts with M or s in the/proc/meminfo file
~]# grep-e "^ (m| S) "/proc/meminfo
memtotal:808160 KB
memfree:488092 KB
swapcached:0 KB
mlocked:0 KB
swaptotal:2072572 KB
swapfree:2072572 KB
mapped:15460 KB
shmem:236 KB
slab:75108 KB
sreclaimable:16084 KB
sunreclaim:59024 KB
Displays lines beginning with a letter in the/etc/inittab file
~]# grep "^[a-z]"/etc/inittab
Id:3:initdefault:
Find an integer between 1-255 in the ifconfig command result
~]# Ifconfig | Grep-e--color "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \> "
Eth0 Link encap:ethernet HWaddr 00:0c:29:12:ba:26
inet addr:172.16.249.45 bcast:172.16.255.255 mask:255.255.0.0
Inet6 ADDR:FE80::20C:29FF:FE12:BA26/64 Scope:link
Up broadcast RUNNING multicast mtu:1500 metric:1
RX bytes:23544203 (22.4 MiB) TX bytes:14827318 (14.1 MiB)
inet addr:127.0.0.1 mask:255.0.0.0
Inet6 addr::: 1/128 scope:host
Up LOOPBACK RUNNING mtu:65536 metric:1
Linux grep command