Grep (Globel Search Regular Expression and Printing out the line) is a powerful text Search tool, it is a search job for operations on rows. It can use regular expressions to search text and print matching rows. Unix grep families include grep, egrep, and fgrep. Egrep indicates the extended grep, which supports more metacharacters than grep. "grep-E" is equivalent to egrep. Fgrep is fast grep and does not support metacharacters, but the search speed is faster. The grep search result is sent to the screen without affecting the content of the original file.
1. grep syntax [] (man grep view grep help document)
Grep [options] 'pattern' FILE
Command Option mode file
Grep directly filters strings without quotation marks. grep must be enclosed by quotation marks when performing mode matching, either single quotation marks or double quotation marks. grep must be enclosed by double quotation marks when referencing variables.
2. grep options [option]
-R: recursive search
-V: reverse selection. Only rows that do not conform to the mode are displayed.
-O: only the strings matched by the pattern are displayed, rather than the entire row.
-I: The matching is case insensitive.
-A #: displays the matched rows. By the way, the # Line (# indicates A value) is displayed)
-B #: the first line #
-C #: front and back # Rows
-E: use an extended regular expression.
Example of the eg: grep Option
cat > eg1.text << EOFThis is firsthow are youHow old are youfine,thankswhat,so whatWhat is your nameEOFgrep "you" eg1.textgrep -o "you" eg1.textgrep -v "you" eg1.textgrep -i "what" eg1.textgrep -A 1 "fine" eg1.textgrep -B 1 "fine" eg1.textgrep -C 1 "fine" eg1.text
The above code is directly pasted and copied on linux and runs directly. The Code explains the running effect as follows:
3,
A regular expression (man regex) is a single string used to describe or match a series of strings that conform to a certain syntax rule. It is usually used to retrieve or replace a string that matches a certain
The text content of the mode. Regular Expressions include basic regular expressions and extended regular expressions.
Metacharacters are special characters that have special meanings in regular expressions.
Grep supports metacharacters of basic Regular Expressions:
^: The content that meets the conditions at the beginning of the anchor line. Format: "^ pattern"
$: The content that meets the conditions at the beginning of the anchor line. Format: "pattern $"
^ $: Matches blank rows
.: Match any single character
*: Match any times (0, 1, multiple times) next to the preceding character)
. *: Match any character of any length
\? : Match 0 or 1 times next to the top character
\ {M, n \}: match the first character at least m times, at most n times
\ {M ,\}: match the character above it at least m times
\ {M \}: exact match of the previous m times
\ {0, n \}: 0 to n times
\ <: Beginning of the anchor-equivalent to \ B. Format: \ <pattern
\>: Anchor end, format: \> pattern
\ <Pattern \>: Word anchor
\ (\): Group, usage format: \ (pattern \), reference the group \ 1 of the first parentheses, the second is \ 2, and so on
[]: Match any single character in the specified range
[^]: Match any single character out of the specified range
Example of basic Regular Expression
(1) display the lines starting with "s" of the/proc/meminfo file with no size difference;
grep "^[sS]" /proc/meminfo
(2) display the rows ending with nologin in/etc/passwd;
grep "nologin$" /etc/passwd
(3) display rows starting with spaces in the/etc/inittab;
grep "^$" /etc/inittab
(4) display the lines starting with r in/etc/passwd followed by any single character;
grep --color "^r." /etc/passwd
(5) display/etc/passwd. Rows starting with r followed by o and o appear at any time;
grep --color "^ro*" /etc/passwd
(6) In the/etc/passwd file, r is followed by any characters in length and h is followed;
grep --color "r.*h" /etc/passwd
(7) in/etc/passwd, r is followed by o, and o appears 0 or 1 times;
grep --color "ro\?" /etc/passwd
(8) in/etc/passwd, r is followed by o, and o appears at least 1 to 2 times;
grep --color "ro\{1,2\}" /etc/passwd
(9) in/etc/passwd, r is followed by o, and o only appears twice;
grep --color "ro\{2\}" /etc/passwd
(10) display the lines matching the word root in/etc/passwd;
grep --color "\<root\>" /etc/passwd
Grep supports the extension expression metacharacters: it supports all the metacharacters of the basic regular expression. Some metacharacters are used differently. The extension Regular Expression command egrep or grep-E
? : Match 0 or 1 times next to the top character
{M, n}: At least m times, at most n times
(): Group
+: Match the preceding characters at least once.
A | B: Match a or B
Example of extended Regular Expression
(1) in/etc/passwd, the line starting with "r" is followed by "o" and "o" appears 0 or once;
egrep --color "ro?" /etc/passwd
(2) in/etc/passwd, the line starting with "r" is followed by "o", and "o" appears at least once and at most twice;
egrep --color "ro{1,2}" /etc/passwd
(3) display/etc/inittab files that start with a number and end with the same number as the starting number;
egrep --color "^([0-9]).*\1$" /etc/inittab
(4) in/etc/passwd, r starts with o and o appears at least once;
egrep --color "ro+" /etc/passwdgrep -E --color "ro{1,}" /etc/passwd
(5) display the lines matching root or halt in/etc/passwd;
egrep --color "root|halt" /etc/passwd
(6) display the rows in the/var/log/secure file that contain "login on" or "Failed passwd;
egrep --color "(LOGIN ON|Failed passwd)" /var/log/secure
Grep supports character and Character Set combination
\ D: digit character matching. It is equivalent to [0-9].
\ S: matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v.
\ S: match any non-blank characters. Equivalent to [^ \ f \ n \ r \ t \ v]
\ W: matches any character type, including underscores. Equivalent to [A-Za-z0-9.
\ W: matches any non-word characters. Equivalent to [^ A-Za-z0-9.
[: Digit:]: All numbers, equivalent to 0-9 or \ d
[: Lower:]: All lowercase letters
[: Upper:]: all uppercase letters
[: Alpha:]: All letters
[: Alnum:]: equivalent to [0-9a-zA-Z]
[: Space:]: The space character is equivalent to \ s.
[: Punct:]: All punctuation marks
Example of Supporting Character Set combination
(1) display/etc/rc. d/rc. sysinit with the start of #, followed by one or more blank characters, followed by any non-blank characters;
grep "^#[[:space:]]\{1,\}[^[:space:]]" /etc/rc.d/rc.sysinitgrep -E "^#\s{1,}\S" /etc/rc.d/rc.sysinit
(2) display/etc/inittab contains: a number (that is, a number between two colons) rows;
grep --color ":[[:digit:]]:" /etc/inittabgrep --color ":\d:" /etc/inittab
Classic example
(1) Grouping example
cat > test.txt <<EOFHe like his likerHe love his loverShe love her loverShe like her loverEOFgrep "l..e.*l..er" test.txtgrep "\(l..e\).*\1r" test.txt
(2) matching numbers 1-
cat > num.txt << EOF12234255256EOFgrep --color -E "\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>" num.txt
(3) match the ABC class IP address, that is, 1.0.0.1 --- 223.20.255.254
cat > ip.txt <<EOF1.0.0.2541.0.0.2551.2.3.4223.255.255.254224.255.255.2522.255.255.255EOFgrep -E --color "\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>" ip.txt
(4) matching Email address: any length of digits and letters @ any length of digits and letters. (com "org | net, etc)
cat > email.txt << EOF5678967@qq.comjie231@sina.cnken_tom@netcom.orgjerry#li@baidu.netli@souhu.netEOFgrep -E --color "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$" email.txt
(5) matching mobile phone number: the mobile phone number is 1 [3 "4 | 5 | 8] followed by 9 digits
cat > tel.txt << EOF1369087689012589098379156087640831582097461913807408271118618203761192097839001329873909EOFgrep --color -E "\<1[3|4|5|8][0-9]{9}\>" tel.txt
Training, ranking first in praise in China, and known as the "Whampoa Military Academy" in the Linux field by netizens. All the courses are explained using Centos6.5x86 _ 64. After summing up and refining several network classes, gradually improving the curriculum system, student learning progress supervision and quality examination system to test the degree of mastery of the students, active online Q & A links, accompanied by famous teachers, cattle give directions, wonderful not to miss.
Details DASH: http://www.magedu.com/
Course: http://www.magedu.com/mentuqc
Network Class integration service: http://mageedu.blog.51cto.com/4265610/1379598
Contact: customer service QQ 2813150558 customer service QQ 1661815153
========================================================== ====