grep command Syntax
The grep command uses an option , a pattern to match , and a file to search , and its syntax is as follows:
grep [Options] PATTERN [FILES]
If no file name is provided, the grep command searches for standard input.
- Options
Options |
Meaning |
-C |
Output matches the number of rows instead of the output matching rows |
-E |
To enable an extended expression |
-H |
Cancels the normal prefix for each output line, which is the file name that matches the query pattern |
-I. |
Ignore case |
-L |
Lists only the file names that contain matching rows, not the true matching rows |
-V |
Reverse the matching pattern, that is, the search does not match the row, not the matching row |
- PATTERN
The matching pattern uses regular expressions. A regular expression is a logical formula for a string operation, which is a "rule string" that is used to express a filter logic for a string, using predefined specific characters and combinations of these specific characters .
Given a regular expression and another string, we can achieve the following purposes:
1. Whether the given string conforms to the filtering logic of the regular expression (called "match");
2. You can get the specific part we want from the string using a regular expression.
Regular expressions are characterized by:
1. Flexibility, logic and functionality are very strong;
2. Complex control of strings can be achieved quickly and in a very simple way.
3. For people who have just come into contact, it is more obscure and difficult to understand.
Because regular expressions are primarily applied to text, they are applied in various text editor situations .
During the use of regular expressions, some characters are handled in a specific way. The following table shows the special characters that are supported and most commonly used by the grep command:
characters |
|
^ |
points to the beginning of a row |
$ |
points to the end of a line |
|
any single character |
[] |
square The number contains a range of characters, where any one of the characters can be matched, such as the character range A~e, or the character range preceded by a ^ symbol to use the inverse character range, which does not match the characters within the specified range. |
If you want to use the above characters as normal characters, you need to precede them with the \ character. For example, if you want to use the $ character, you need to write it as \$.
Some useful special matching patterns can also be used in square brackets. As shown in the following table:
Matching mode |
Meaning |
[: Alnum:] |
Letters and numeric characters |
[: Alpha:] |
Letters |
[: ASCII:] |
ASCII characters |
[: Blank:] |
Spaces or tabs |
[: Cntrl:] |
ASCII control characters |
[:d Igit:] |
Digital |
[: Graph:] |
Non-controlled, non-whitespace characters |
[: Lower:] |
lowercase letters |
[:p rint:] |
printable characters |
[:p UNCT:] |
Punctuation characters |
[: Space:] |
whitespace characters, including vertical tab characters |
[: Upper:] |
Capital |
[: Xdigit:] |
hexadecimal digits |
In addition, if you specify the-e option for extended matching, those other characters that control the completion of the match may follow the rules of the regular expression, as shown in the following table:
Options |
Meaning |
? |
Match is optional, but matches at most once |
* |
Must match 0 or more times |
+ |
Must match 1 or more times |
N |
Must match n times |
{N,} |
Must match N or n times |
{N,m} |
The number of matches is between N and M, including N and M |
For grep commands in the shell, you need to precede these characters with the \ character.
grep Command Example
- The first example is finding the line that ends with the letter E.
<span style= "FONT-SIZE:12PX;" >grep e$ words.txt</span>
- Finds words that end with the letter A.
<span style= "FONT-SIZE:12PX;" >grep A[[:blank:] words2.txt</span>
- Finds a 3-letter word that begins with th.
<span style= "FONT-SIZE:12PX;" >grep Th.[[:space:] words2.txt</span>
grep commands in Shell script programming