-: Common wildcards:*? []
*: Match any character of any length
? : Match any character
[]: Match any single character in the specified range
[^]: Delimiters, indicating inverse.
Ii. Regular Expression: Is a search and string replacement operation. It can be divided into basic regular expressions and extended regular expressions. Regular Expressions are widely used in text editors. For example, regular expressions are used in:
Check whether the text contains the specified Feature Word
Locate the location of matched feature words in the text
Extract information from text, such as a substring of a string.
Modify text
Basic Regular Expression:
Metacharacters: do not represent the meaning of the character itself, used for additional functional descriptions.
Metacharacters of the basic regular expression:
Character match:
.: Any single character
[]: Any single character in the specified range
[0-9], [[: digit:]
[A-Z], [[: lower:]
[A-Z], [[: Upper:]
[[: Alpha:] all uppercase and lowercase letters
[[: Alnum:] contains numbers and letters
[[: Space:] All blank characters
[[: Punct:] All punctuation marks
[^]: Any single character out of the specified range
Number of times: used to specify the number of times to match the character before it
*: Any time
Example: x * y, XXY, XY, y,
. *: Match any character of any length
\? : 0 or 1 time
X \? Y, XY, Y, XXY
Greedy mode: Match characters as long as possible;
\ {M \}: matching m times
\ {M, N \}:
\ {M, \}: At least m times;
\ {0, n \}: up to n times;
Position anchoring: used to specify the position where a character appears
^: Pin the beginning of a line
^ Char
$: End of the anchor row
Char $
^ $: Blank line
\ <CHAR: the beginning of the anchor, \ bchar
Char \>: Specifies the end of the anchor, char \ B
GROUP :\(\)
\ (AB \) * XY
Reference: \ 1, \ 2, \ 3... backward reference: refers to the first 1st, 2nd, 3rd from left to right... left brackets and the matching content of the pattern in the right brackets.
Grep:
The text search tool performs a row-by-row search on the target file based on the text mode specified by the user to display the rows that can be matched by the mode.
Format: grep [Options] 'pattern' filename ,...
Common grep options:
-V: reverse: displays the rows that cannot be matched by the mode;
-O: only the strings matched by the pattern are displayed, not the whole line;
-I: Case Insensitive, ignore-case
-E: supports extended regular expressions.
-A #: display matching rows and the following # Rows
-B #
-C # Show matching rows and upper and lower # Rows
-C: count the number of matched rows
-W: Specifies the word to be searched.
-N: add the row number in the file to the front of each output row.
-E: Specify Multiple matching modes.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/E3/wKiom1QC5XHw2JE_AACYeAFmEKQ982.jpg "Title =" qq20140831170416.png "alt =" wkiom1qc5xhw2je_aacyeafmekq982.jpg "/>
-- Color = auto
Exercise:
1. display the rows starting with case s in the/proc/meminfo file;
# Grep "^ [ss]"/proc/meminfo
# Grep-I "^ s"/proc/meminfo
2. Retrieve users whose default shell is not bash;
# Grep-V "bash $"/etc/passwd | cut-D:-F1
3. Obtain the user with the largest ID number and Bash as the default shell;
# Grep "bash $"/etc/passwd | sort-n-t:-K3 | tail-1 | cut-D:-F1
4. the/etc/rc. d/rc. sysinit file starts with #, followed by at least one blank character, and then contains at least one non-blank line;
# Grep "^ # [[: Space:] \ {1, \} [^ [: Space:] \ {1, \}"/etc/rc. d/RC. sysinit
5. display the lines starting with at least one blank character in/boot/GRUB/grub. conf;
# Grep "^ [[: Space:] \ {1, \} [^ [: Space:] \ {1, \}"/boot/GRUB/grub. conf
6. Find one or two digits in the/etc/passwd file;
# Grep -- color = auto "\ <[0-9] \ {1, 2 \} \>"/etc/passwd
7. Locate the integer between 1 and 255 in the ifconfig command result;
# Ifconfig | grep-e -- color = auto "\ <([1-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-5]) \>"
8. view all the information of the root user on the current system;
# Grep "^ Root \>"/etc/passwd
9. Add bash, testbash, and basher users, and find the users with the same username as the default shell on the current system;
# Grep -- color = auto "^ \ ([[: alnum:] \ {1, \ }\) \>. * \ 1 $"/etc/passwd
10. Find the line ending with "listen" or "established" in the result of the netstat-tan command execution;
11. Retrieve the shells of all users on the current system. The requirement is that each shell is displayed only once and in ascending order;
# Cut-D:-F7/etc/passwd | sort-u
2. Extended Regular Expression
Egrep: uses an extended regular expression to construct the mode, which is equivalent to grep-e.
Character match:
.: Any single character
[]: Any single character in the specified range
[^]: Any single character out of the specified range
Matching times:
*: Match any character before it;
? : Match the first character 0 or 1 time;
+: Match the first character at least once
{M}: match the previous characters m times;
{M, n}: At least m times, at most N times
{M ,}: at least m times;
{0, n}: up to n times;
Anchored:
^: Beginning of a row
$: End of a row
\ <, \ B: Beginning of the word
\>, \ B: End
GROUP:
(): Group
|: Or, c | cat matches C or cat
(C | C) at matches cat or cat
Note: using an extended regular expression, which is in the basic regular expression ?, +, {}, () And so on do not need to use escape characters \
This article from "happy every day" blog, please be sure to keep this source http://as007012.blog.51cto.com/6215447/1547124
Grep and egrep of text processing tools