I. Introduction to 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. The use of regular expressions can achieve powerful text processing. The following example describes the regular expression.
Ii. Classification of text processing tools
Commonly used are grep, egrep, and fgrep.
Differences:
Grep: If no parameter exists, only the RE (Regular Expression) characters are output.
Egrep: equivalent to grep-E. The biggest difference between grep and grep is the Escape Character. For example, when grep matches the number of times, \ {n, m \} egrep does not need to be directly {n, m }. Egrep is convenient.
Fgrep: equivalent to grep-f, but cannot use a regular expression. All character matching functions have been removed.
3. grep parameter description:
Format: grep [OPTIONS] PATTERN (mode) [FILE...]
Common options:
-- Color = auto: displays the color parameters.
-N: Output row number.
-V: reverse match, indicating the rows that cannot be matched by the pattern. For example, if root is specified, the matching result is not root.
Example:
-O: only the specified characters matching the pattern are displayed.
-I: case-insensitive characters.
-A #: not only can match the specified character line. The specified n rows after the character can also be displayed.
-B #: not only can match the specified character line. The specified n rows before the character can also be displayed.
-C #: not only can match the specified character line. The specified n rows before and after the character can also be displayed.
For the convenience of defining grep -- color = auto as an alias, the alias is cgrep. Easy to use. Then you can use cgrep to display the color.
Alias cgrep = "grep -- color = auto"
4. Common Character matching and examples:
When character matching and metacharacters are used, quotation marks must be used;
Use single or double quotes.
.: Any single character refers to a point representing 1 character. This character is not special to the minute, And the symbol is also a line.
[]: Any single character in the specified range. The specified characters in the brackets can be matched and the matching range is wide.
[^]: Specifies any single character out of the range, and the above [] idea. . The common meaning is that the characters specified in parentheses cannot be matched.