Grep, egrep, fgrep
Grep: searches for text based on the mode and displays the line of text matching the mode.
Summary:
1. Common grep Parameters
2. grep Regular Expression meta-character set (basic usage) and advanced usage
3. POSIX special character classes related to regular expressions
Syntax: grep [options] PATTERN [FILE...]
Common grep parameters:
-R recursive matching
-E advanced scalability is similar to egrep Function
-I ignore case differences
-V reverse match: Only unmatched rows are displayed.
-If w is referenced by \ <and \>, the expression is used as a word search (word matching)
For example, netstat-ntlp | grep "\ <80 \>"
Grep-w 'main'/usr/include/*. h
-N: display matching rows and row numbers
-L used for searching multiple files. When querying multiple files, only the names containing matching characters are output.
Grep-l-r-I-w 'filename_max '/usr/include/*. h
-- Color highlight matching
-C: only the number of matched rows is printed, and the matching content is not displayed.
-O: only the strings matching the pattern are displayed.
Grep Regular Expression metacharacters (basic usage)
Matching times (Greedy mode ):
. Match a non-linefeed character such as: 'gr. P' match gr followed by any character, followed by p.
* Match zero or multiple previous characters, for example, '* grep'. Match All one or more spaces followed by the grep line.
. * Represents any character.
\? Match the first character once or 0.
X \ {m \} repeats the characters x and m consecutively. For example, 'O \ {5 \} 'matches the rows that contain 5 consecutive o Records.
X \ {m, \} repeats the character x continuously for at least m times, for example, 'O \ {5, \} 'matches rows with at least 5 o consecutively.
X \ {m, n \} repeats the character x consecutively, at least m times, no more than n times, for example, 'O \ {5, 10 \} 'matches rows of 5-10 consecutive o.
Metacharacters:
[] Matches a character in a specified range, for example, '[Gg] rep' matches Grep and grep.
[^] Match a character that is not within the specified range, for example, '[^ A-FH-Z] rep' match a line that does not start with a letter that does not contain the A-R and T-Z, followed by rep.
Positioning:
^: Specifies the beginning of the line. Any content after this character must appear at the beginning of the line.
$: Specifies the end of the line. Any content before this character must appear at the end of the line.
\ <Or \ B specifies the start of a word, for example, '\ <grep' matches a row that contains a word starting with grep.
\> Or \ B specifies the end of a word, for example, 'grep \> 'matches a row that contains a word ending with grep.
GROUP:
\(\)
\ (AB \)*
Backward reference
\ 1: reference the first left brace and all content contained in the corresponding right Brace
\ 2:
\ 3:
\ W matches a character with a number, that is, a [A-Za-z0-9], for example, 'G \ w * P' matches a character with G followed by zero or more characters or numbers, then p.
The inverse form of \ W w. It matches a non-word character, such as a period ending. \ W * can match multiple.
The \ B word lock. For example, '\ bgrep \ B' only matches grep, that is, it can only be a grep word, with spaces on both sides.
Search for multiple keywords
1.1 Use-e
Grep-e 'stdio \. H'-e 'stdlib \. H'/usr/include/*. h
1.2 use-f file
Store keywords in a file as rows
1.3 Use metacharacters \ |
Grep 'stdio \. h \ | stdlib. H'/usr/include/*. h
Advanced usage of egrep and grep-E:
+ Match one or more previous characters. For example, '[a-z] + able' matches one or more lower-case letters followed by able strings, such as loveable, enable, and disable.
? Matches zero or multiple previous characters. For example, 'gr? P' matches gr followed by one or no characters, and then the row of p. A | B | c matches a, B, or c. For example, grep | sed matches grep or sed.
() Grouping symbols, such as: love (able | rs) ov + matches loveable or lovers and matches one or more ov. X, x {m,}, x {m, n} act the same as x \ {m \}, x \ {m, \}, x \ {m, n \}
POSIX special character class: You can view it in man 7 glob...
[: Alpha:] character [A-Za-z]
[: Digit:] digit character [0-9]
[: Graph:] non-empty characters (non-space, control characters)
[: Lower:] lowercase character [a-z]
[: Upper:] uppercase character [A-Z]
[: Alnum:] [0-9a-zA-Z]
[: Cntrl:] control characters
[: Print:] non-empty characters (including spaces)
[: Punct:] punctuation marks
[: Space:] All blank characters (new lines, spaces, and tabs) can also be represented by \ t.
[: Xdigit:] hexadecimal numbers and letters (0-9, a-f, A-F)
We need to use [[: alpha:] In this way. If the inverse is [^ [: digit:].