grep
1 grep [Options] PATTERN [FILE ...] 2 grep [Options] [-E PATTERN |-F FILE] [FILE ...]
1. grep's regular Expression
REF[1]
A): Match single character
The basic building block in Reg EX is a reg ex that matches a single character. Most characters (including numbers and letters) are matched to their own Reg ex.
B): meta-character
Any metacharacters (metacharacter) that has a special meaning is identified by a leading backslash (backslash, i.e.: \).
C): Bracket expression
Bracket expression is a list of characters surrounded by []. Such expressions match any one of the characters in the list (note that only one character is matched).
If ^ is the first character in a character list, the expression matches any character that is not in the list.
For example: [0123456789] matches any a single number; [A-z] matches any lowercase letter.
To match the character '] ', you need to place '] ' in the first position of the word list characters. Example: []a-z]
A similar to match ' ^ ' needs to place ' ^ ' in the first position of the character list. Example: [a^]
Similar to match '-' need to put '-' in the last position of the character list. Example: [A]
D): Named character category (named classes of characters)
[: Alnum:] [: Alpha:] [: Cntrl:] [:d Igit:] [: Graph:] [: Lower:] [:p rint:] [:p UNCT:] [: Space:] [: Upper:] [: Xdigit:]
E): '. ' Dot matches any single character. \w and [[: Alnum:]] synonymous, \w and [^[:alnum:]] synonymous.
F): ^ and $ match an empty string for the beginning and end of a line, respectively. For example:
$ grep ^line1 Data
Matches the line that begins with "Line1" in the file data.
G): \< \>
The symbols \< and \> respectively match the empty string at the beginning and end of a word.
H): \b \b
The symbol \b matches the empty string at the edge of a word, and \b matches the empty string
Provided it ' s not at the edge of a word.
I): Repeat operator (repetition operator)
Repetition operator
? |
The preceding item is optional and matched at most once. |
* |
The preceding item would be matched zero or more times. |
+ |
The preceding item would be a matched one or more times. |
N |
The preceding item is matched exactly n times. |
{N,} |
The preceding item is a matched n or more times. |
{N,m} |
The preceding item is a matched at least n times, and not more than m times. |
J): Match meta-character *
?, +, {, |, (, and) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \ (, and \)
K): Reg EX Operation: concatenation, join
Regular expressions may concatenated; The resulting regular expression matches any string formed
By concatenating, substrings that respectively match the concatenated subexpressions.
Regular expressions is joined by the infix operator |; The resulting regular expression matches
Any string matching either subex-pression.
Repetition takes precedence over concatenation, which on turn takes precedence over alternation.
A whole subexpression is enclosed in parentheses to override these precedence rules.
The backreference \ n, where n is a single digit, matches the substring previously matched by the nth parenthesized
subexpression of the regu-lar expression.
For example: $ grep "Line1\|line2" data
Matches rows that contain "line1" or "line2".
2. grep's example
[Todo]: ref[2]
Reference
1. grep
Http://unixhelp.ed.ac.uk/CGI/man-cgi?grep
2. Advanced Regular Expressions in Grep Command with ten Examples–part II
http://www.thegeekstuff.com/2011/01/advanced-regular-expressions-in-grep-command-with-10-examples-%E2%80%93-part-ii/
Linux.CommanlineTool.grep