Grep is one of the most widely used commands in Linux. It is abbreviated as globally search a regular expression and print and is a powerful text search tool. Grep supports text search using regular expressions. The target file is searched row by row based on the mode specified by the user and the matched rows are displayed. By default, the results are printed to the standard output.
Linux uses the GNU version of grep, including grep, egrep, and fgrep3 formats:
Grep: Standard grep, supports regular expressions.
Egrep: an extended regular expression that supports basic and extended regular expressions. Generally, it can be equivalent to grep-e.
Fgrep: Quick grep, which only allows searching strings rather than modes. Therefore, regular expressions are not allowed, which is equivalent to grep-f.
Grep Syntax:
Grep [-options] pattern [file…]
Common options of grep
-C: displays the number of rows matching the row instead of the row itself.
-Case Insensitive for I matching
-N: displays the number of lines in the file that match the content.
-L when searching for multiple files, the file containing matching rows is displayed, rather than the row itself.
-L is opposite to-L, displaying files that do not contain matching rows
-R recursive search to find this directory tree
-Q silent output, often used for Shell basic condition judgment
The pattern used by grep is usually a regular expression, so to use grep skillfully, you must learn to use regular expressions.
A regular expression is an indirect method that specifies the string mode. It is usually abbreviated as RegEx or Re.
The main difference between the basic regular expression used by grep and egrep: Basic regular expression ?, +, | Cannot be used, (), {} must use/escape.
For GNU grep, the extended regular expression is more practical, so you can create an alias and add the E Option for grep by default.
Alias grep = 'egrep 'or alias grep = 'grep E'
If you want to make the alias take effect permanently, you can write it to the configuration file ~ /. Bashrc
The following is the syntax of the commonly used egrep regular expression:
Metacharacters |
Description |
Example |
^ |
First line anchor |
^ Love matches all rows starting with love |
$ |
Anchor at the end of a line |
Love $ match all rows ending with love |
. |
Match any single character |
Lov. Match Lova, lovb, or love. |
[] |
Match a single character contained in [] |
Love [ABC] matches Lovea, loveb or lovec |
/< |
Pin the beginning of a word |
/<Love matches the position starting with love |
>/ |
Pin the end of a word |
Love>/match the end of love |
[^] |
Match a single character not included in [] |
Love [^ 12] matches from love3 to love9 |
[-] |
Match a single character in the range specified [] |
[1-5] matches one character in 1, 2, 3, 4, 5. |
| |
Logical operator or match | any item on both sides |
Love (A | B) matches Lovea or loveb |
\ |
Escape special characters |
Lov \. e matches lov. e instead of lovae, lovbe |
() |
Create a matched string |
([0-9] {1, 3} \.) {3} [0-9] {1, 3} matches the IP address |
Regular Expression: Basic match
Operator |
Description |
Example |
? |
Match the previous item 0 or 1 time |
Love? Lovee cannot be matched if it matches lov or love. |
+ |
Match the previous item once or multiple times |
Love + matches love or lovee cannot match lov |
* |
Match the previous item 0 or multiple times |
Love * matches lov, love, or lovee |
{N} |
Match the previous item n times |
[0-9] {3} matches any three-digit number |
{N ,} |
Match the first item at least N times |
[0-9] {3,} matches any three-digit number or more digits |
{, N} |
Match the first item up to n times |
[0-9] {, 2} matches any one-digit or two-digit number. |
{M, n} |
Match the previous item at least m times, up to n times |
[0-9] {} matches the number of any one from two digits to five digits. |
Regular Expression: Number Operator
Class |
Description |
Similar |
[: Lower:] |
Lowercase letters |
A-z |
[: Upper:] |
Uppercase letters |
A-Z |
[: Alpha:] |
Uppercase/lowercase letters |
A-Za-z |
[: Digit:] |
Number |
0-9 |
[: Alnum:] |
Uppercase and lowercase letters and numbers |
A-zA-Z0-9 |
[: Punct:] |
Punctuation Marks
|
|
[: Black:] |
Spaces and tabs |
|
This article is from the "Linux" blog, please be sure to keep this source http://landy.blog.51cto.com/8910973/1567733
Use of grep and egrep