Grep is a common command in Linux. Its main function is to compare string data, use regular expressions to search for text, and print the strings that meet your needs. Grep stands for global regular expression print, which indicates the global regular expression version. Its permission is granted to all users. When grep finds a string in the data query, it selects data based on the unit of full behavior.
(1) Command Format
Grep [cinvs] 'pattern' filename
(2) Main Parameters
-C: only counts matching rows are output.
-I: case-insensitive (only applicable to single characters ).
-N: displays matching rows and row numbers.
-S: the error message that does not exist or does not match the text is not displayed.
-V: displays all rows that do not contain matched text.
(3) Main Parameters of the Patten Regular Expression
\: Escape Character. Ignore the original meaning of special characters in a regular expression.
^: Match the rows starting with a string.
$: Match the row ending with a string.
\ <: Starts from the row that matches the regular expression.
\>: Ends with the row that matches the regular expression.
[]: A single character in []. For example, [a] indicates that a meets the requirements.
[-]: A range character marked by [-], such as [A-Z], that is, a, B, c Until Z all meet the requirements.
.: It must contain 1 arbitrary character.
*: Repeats the first 0 or multiple characters.
There are many more parameters for regular expressions. This is a small part of the list. For details, refer to the regular expressions.
(4) Instances
To make good use of the grep tool, we need to write a regular expression. Therefore, we will not explain all the functions of grep here. We will only list a few examples to illustrate how to write a regular expression.
$ LS-L | grep '^'
Filter the LS-L output content in the MPs queue and display only the rows starting with.
$ Grep 'test' AA BB CC
The row Matching Test is displayed in the AA, BB, and CC files.
$ Grep '\ {5 \}' AA
Display All rows of a string that contains at least five consecutive lowercase characters.
$ Grep 'W \ (ES \) T. * 'AA
Display All rows that contain west and then zero or multiple arbitrary characters.