Grep, regular expression, and grep Regular Expression
Grep: a text search tool that checks the row-by-row matching of the target file based on the user-specified "Mode": prints the matched rows.
Pattern: filtering condition for writing regular expressions.
Regular Expression (REGEXP): A pattern written by a special character or text character. Some of these characters do not represent the literal meaning of the character, but represent the control or wildcard function.
There are two types:
Basic Regular Expression: BRE
Extended Regular Expression: ERE (grep-E, egrep)
Use of grep:
Grep [Option] mode [file...]
Option:
-- Color = auto: color the matched text;
-V: displays the rows that are not matched by the "mode;
-I: case-insensitive characters are ignored;
-O: only the matched strings are displayed;
-Q: In silent mode, no information is output;
-A #: display matching rows and backend # Rows;
-B #: displays matching rows and the first # rows;
-C #: displays matching rows and front and back # rows;
-E: Use ERE (equivalent to egrep)
Basic Regular Expression metacharacters:
Character match:
.: Match any single character;
[]: Match any single character in the specified range
[^]: Match any single character out of the specified range
[: Alnum:]: represents English case characters and numbers, and a-z, A-Z, 1-9
[: Alpha:]: stands for English case characters, and a-z, A-Z
[: Cntrl:]: indicates the control buttons, such as CR, LF, Tab, and Del.
[: Digit:]: represents a number, 1-9
[: Graph:]: All keys except space and Tab
[: Lower:]: lowercase letter, a-z
[: Print:]: represents any character that can be printed.
[: Punct:]: stands for punctuation marks, that is "'?!; : # $
[: Space:]: any character that produces white space, including space key [Tab] CR
[: Upper:]: stands for capital letters, A-Z
[: Xdigit:]: represents the hexadecimal number type, so contains 0-9, A-F, a-f digits and characters
Note: The preceding [: ***:] must be used in double brackets, that is, [[[: ***:]
Matching times:
*: Matches the previous character 0 or multiple times;
.: Match any single character;
. *: Matches any character 0 or multiple times;
\? : Match the first character 0 or 1 time;
\ +: Match the previous character once or multiple times;
\ {M \}: match the previous characters m times;
\ {M, n \}: match the previous character at least m times, at most n times;
\ {0, n}: match the preceding characters up to n times;
\ {M ,\}: match the previous character at least m times;
Positioning:
^: The first line is anchored to the left of the mode;
$: The end of the line is anchored to the rightmost of the mode;
\ <Or \ B: the beginning of the word, used on the left side of the word mode;
\> Or \ B: The end of the word, used to the right of the word mode
Group and Back Reference:
\ (\): Binds one or more characters for processing as a whole.
Backward reference: Apply the pattern matching characters (not the pattern itself), \ 1, \ 2... \ n
For example, \ (abc * \ (def \) ghe \? \)
\ 1: abc * \ (def \) ghe \? Corresponding to the content in the brackets where the first left parenthesis is located
\ 2: def corresponds to the content in the brackets of the second left bracket
Extended Regular Expression metacharacters:
Same as the regular expression: the difference is that special symbols do not need to be escaped using .? Equal \?, () Equals \ (\), {m} equals \ {m \}, and + equals \ +. Note: \ <, \>, \ B still needs to add "\"
N | m: | match all content on the left or right
For example:
Abc | def matches abc or def. By default, all characters on the left and right sides are matched and can be grouped by (), as shown below:
AB (c | de) f matches c | de