1. in Linux, the grep command is a powerful text search tool that uses regular expressions to search for text and print matching lines. Grep stands for GlobalRegularExpressionPrint, which indicates the global regular expression version.
1. Role
In Linux, the grep command is a powerful text search tool that uses regular expressions to search for text and print matching lines. Grep stands for Global Regular Expression Print, which indicates the Global Regular Expression version. its permission is granted to all users.
2. Format
Grep [options]
3. main parameters
[Options] main parameters:
-C: only counts matching rows are output.
-I: it is case-insensitive (only applicable to single characters ).
-H: when querying multiple files, the file name is not displayed.
-L: when querying multiple files, only names containing matching characters are output.
-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.
Main parameters of the regular expression pattern:
\: Ignore the original meaning of special characters in regular expressions. ^: Match the start line of the regular expression.
$: Matches the end row of the regular expression.
\ <: Starts from the row that matches the regular expression.
\>: Ends with the row that matches the regular expression.
[]: A single character. for example, [A] indicates that A meets the requirements.
[-]: Range, such as [A-Z], that is, A, B, C until Z all meet the requirements.
.: All single characters.
*: All characters. The length can be 0.
{N}: must match n times.
{N ,}: must match n or more times.
{N, m}: the number of matching times is between n and m, including n and m.
4. instance
1. output the row number with ""
$ Grep-n 'the 'a.txt
2. the output does not contain the "the" row number.
$ Grep-nv 'The a.txt
3. use [] to search for a set element, and [] to represent any character. for example, [abc] can represent a, B, or c.
$ Grep-N'T [abc] AB 'a.txt
You can use the ^ symbol as the prefix in [] to indicate characters other than the characters in. For example, to search for the row where no string of g is located before object oo, use '[^ g] oo' to search for the string.
$ Grep-n' [^ g] oo 'a.txt
[] Can be expressed in a range, such as [a-z] for lowercase characters, [0-9] for 0-9 numbers, [A-Z] for uppercase letters. A-zA-Z0-9
$ Grep-n't [] AB 'a.txt
4. search for empty rows. '^ $' is used to indicate empty rows with only the beginning and end of the row
$ Grep-n' ^ $ 'a.txt
5. search for non-empty rows. use '^ $' to indicate empty rows with only the beginning and end of the row
$ Grep-nv '^ $' a.txt
6. search for rows containing two oo
$ Grep-n 'O \ {\ 2 \} 'a.txt
7. count the number of "the" included
$ Grep-c 'The a.txt