The name grep, egrep, fgerp explains: grep is a command line tool originally used for UNIX operating systems. after a file list or standard input is provided, grep searches for the text that matches one or more regular expressions and outputs only the rows or text that match (or do not match. usage: grep [Option] 'pattern' file... -- color = auto
Common options [Option]:
-V: reverse match. lines that cannot be matched by the pattern are displayed.
-O: only the strings matched by the pattern are displayed, not the whole line.
-I: case-insensitive
-E: supports extended regular expressions.
-A number: (After) displays the row matched by the pattern and the row below.
-B Number: (Before) display the row matched by the pattern and the row above
-C Number: (context) display the rows matched by the pattern, and display the rows of the context-N: the row number displayed in the text-R: recursive search
Example |
Release |
Egrep-I 'test' path/to/File |
Search the case-insensitive 'test' string in the file and print the row. |
Egrep-o "[a-z0-9 _] [email protected] [a-z0-9] + \. [A-Z] +"/path/to/File |
Output all email addresses in the file, only content |
Egrep-r-n "test_function ()". |
Search the sub-files in the current directory and find the row number and content of the file where the output content is located. |
Extended Regular Expression 'pattern': egrep uses an extended regular expression to construct a pattern, which is equivalent to the basic component of grep-e.
Regular Expression |
Description |
Example |
. |
Match any single character |
Hack. Matches Hackl and hackm, but cannot match hackll, hack |
[] |
Match any character contained in [character] |
Coo [Kl] matches cool or cook |
[^] |
Match any character except [character] |
9 [^ 01] match but not 91, 90 |
[-] |
Match any character in the range specified in [] |
[0-3] match 1 ~ 3. Any number |
^ |
Start of the anchor row |
^ Diguo matches the rows starting with diguo. |
$ |
End of the anchor row |
Diguo $ match the row ending with diguo |
\ <Char |
Start of the anchor word |
\ <Char matches words starting with Char |
Char> |
The end of the anchor word |
Char \> match words ending with Char |
* |
Match the previous project 0 or multiple times |
Co * L equals to Cl, Col, cooooool, etc. |
? |
Match the previous project 0 times or 1 time |
X? Y, equal to XY, X |
+ |
Match the previous project once or multiple times |
Rollno-9 + match rollno-99, rollno-9, etc. but cannot match rollno- |
() |
Create a matching string |
MA (TRI )? X matches Max or matrix. |
{M} |
Match the previous project m times |
[0-9] {3} matches any three-digit number. [0-9] [0-9] [0-9] can also be used. |
{M ,} |
Match the previous project at least m times |
[0-9] {2,} matches a 2-digit or more-digit word |
{M, n} |
Match the previous project at least m times, at most N times |
[0-9] {2, 3} matches a 2-digit number or 3-digit number. |
(M | N) |
Or, match | any item on both sides |
Oct (1st | 2nd) matches Oct 1st or Oct 2nd |
/ |
Escape characters to escape the special characters above |
A \. B matches a. B, but cannot match ajb. The special meaning of. is ignored by adding \ before. |
POSIX character class |
Meta sequence [: xxxx:] |
Can match to the common character range |
Regular Expression |
Description |
Example |
[: Alnum:] |
Letters and numbers |
[[: Alnum:] + one or more digits or letters |
[: Alpha:] |
Letter (including uppercase and lowercase letters) |
[[: Alpha:] {4} 4 case-insensitive letters |
[: Blank:] |
Spaces and tabs |
[[: Blank:] * 0 or multiple spaces or tabs |
[: Digit:] |
Numeric characters |
[[: Digit:]? 0 or 1 digit |
[: Lower:] |
Lowercase letters |
[[: Lower:] {5,} a string consisting of at least five lower-case letters |
[: Upper:] |
Uppercase letters |
[[: Upper:] {0, 5} a string consisting of up to five uppercase letters |
[: Punct:] |
Punctuation Marks |
[[: Punct:] a punctuation mark |
[: Space:] |
All blank characters including linefeeds and carriage returns |
[[: Space:] * 0 or multiple blank characters |