I. Regular Expressions
Regular Expression mode: greedy mode, non-Greedy Mode
Regular Expressions include basic regular expressions and extended regular expressions.
1. Basic Regular Expression
| ^ |
Specifies the content at the beginning of a line, such as ^ pattern. |
| $ |
Anchor specifies the content at the end of a line, for example, pattern $ |
| ^ $ |
Blank line |
| . |
Match any single character |
| * |
Match any character on the left |
| .* |
Match any character of any length |
| [] |
Match any single character in the specified range |
| [^] |
Matches any single character out of the specified range |
| \? |
Match 0 or 1 character on the left |
| \ {M, n \} |
Match the character on the left at least m times, at most n times |
| \ {M ,\} |
Match at least m characters on the left |
| \ {M \} |
Match the exact m characters on the left |
\ <
|
The beginning of the anchor, for example, \ <pattern |
| \> |
Pin the end of a word, for example, pattern \> |
| \ <\> |
Anchoring a word is not an English word in the true sense), \ <pattern \> |
| \ B |
Character boundary, similar to the \ <, \> function, such as: \ bpattern \ B anchored word)
|
| \ (Pattern \) |
Group, for example, \ (abc \). * \ 1 Description: here 1 represents 1st groups |
[: Lower:]
|
Lowercase letters |
| [: Upper:] |
Uppercase/lowercase letters |
| [: Digit:] |
Number |
| [: Alpha:] |
Letter |
| [: Alnum:] |
Letters and numbers |
| [: Space:] |
Space |
| [: Punct:] |
Letters other than letters, numbers, controls, and blank spaces) |
2. Extended Regular Expressions
| + |
Repeat one or more previous characters |
| ? |
Zero or one previous character
|
| | |
Or, select one more
|
| () |
Group
|
| () + |
Repeat one or more previous Groups |
| {M, n} |
Match the character on the left at least m times, at most n times |
| \ <\> |
Pin words |
It also includes all basic regular expressions. Except \ <and \>, backlash is not required in extended regular expressions.
For more regular expressions, see man 7 regex.
Can also refer to: http://www.cnblogs.com/lcf/articles/807523.html
Ii. Use grep
Grep supports basic regular expressions.
Egrep supports extension of the regular expression equivalent to: grep-E)
Fgrep does not support pattern matching, but it is fast
Command: grep
Purpose: print the row matching the pattern.
Parameters:
| -- Color = auto |
The matched information is marked with colors. The auto option can be omitted. Modify the default color scheme (foreground and background): export GREP_COLOR = '01; 36' |
| -V |
Reflection Selection |
| -O |
Only the strings matched by the pattern are displayed. |
| -I |
Case Insensitive |
| -W |
Match words |
| -R |
Recursive search |
| -# |
Displays the matched rows and the # Rows following them. |
| -B # |
Show the matched rows and the # Rows in front of them |
| -C # |
Displays the matched rows and the # Rows before and after the matched rows. |
| -E |
Use extended Regular Expressions |
Iii. Example:
Matched IP Address:
Ifconfig | egrep -- color "([1-9] | [1-9] [0-9] | 1 [0-9] {1} | 2 [0-4] [0-9] | 25 [0-5]) \. ([0-9] | [1-9] [0-9] | 1 [0-9] [0-9] | 2 [0-4] [0-9- 9] | 25 [0-5]) \.) {2} ([1-9] | [1-9] [0-9] | 1 [0-9] {1} | 2 [0-4] [0- 9] | 25 [0-5])"
Iv. Notes
1. grep basic regular expression) in use, the indicator must be added with a backslash (\), while the egrep extension Regular Expression) but cannot be added with a backslash \:
Grep -- color "\ (error \)"*
Egrep -- color "(error )"*
Egrep -- color "\ <(error)" * \ <, \>: Except for word anchoring)
This article from the "Linux/Unix O & M Technology" blog, please be sure to keep this source http://wangyc.blog.51cto.com/31193/1304976