[Grep introduction:]
Grep is used to filter rows containing specific characters. It can use regular expressions to search for text. When searching for a string in data, it selects data in the unit of positive behavior. usage: grep [cinvs] 'pattern' file for example: grep-n 'bbb' aaa.txt -- color # search for the keyword bbb from the file aaa.txt, and display the row number and highlighted display.
[Common options:]
-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.
[Regular metacharacters:]
Grep can be used in combination with regular expressions. The following describes some common regular expressions used to filter characters. \: 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.
\ <: Match the start of a word.
\>: The end of the matching word.
[]: 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.
*: The preceding characters can appear at any time.
? : The first character appears 0 or 1 time.
+: The preceding character appears once or multiple times.
[Regular Expression example:]
1. ^ indicates grep '^ bbb' aaa.txt -- color # searches for rows starting with bbb from aaa.txt.
2. $ indicates grep 'bbb $ 'aaa.txt -- color # searches for the row ending with bbb from the aaa.txt file.
3.
.Represents a single character. It can match all characters except line breaks. Grep 'bbb... 'aaa.txt -- color # From the file aaa.txt, find the row that contains three characters After bbb. The 'point' can match spaces.
4. * indicates that the character before it can appear any times grep 'bbb. * 'aaa.txt -- color # searches for lines with any characters After bbb from aaa.txt.
5. + indicates that the character before it must appear at least once grep-E 'bbb. + 'aaa.txt -- color # searches for at least one character line after bbb from the aaa.txt file.
6 .? Indicates whether or not the character before it can exist.
Grep-E 'bbbc? 'Aaa.txt -- color # Find the row with bbb or bbbc from the aaa.txt file. Note: grep does not support + ,? If you want to use these two metacharacters, you can only use extended grep (egrep or grep-E)
7. [] indicates matching a character, grep '^ [Bb] bb 'aaa.txt -- color # searches for rows starting with Bbb or bbb from aaa.txt. Grep '[0-9] 'aaa.txt -- color # search for rows with numbers from aaa.txt' \ d' aaa.txt -- color # search for rows with numbers from aaa.txt, -P indicates that the regular expression \ d of perl is used to represent numbers, and \ D indicates non-numbers \ s indicates spaces or tabs, \ S indicates that no space or tab \ w indicates any characters (uppercase/lowercase letters, numbers, and underscores), and \ W indicates that it is not a letter, number, or underline.
Note: ^ if it appears outside [], it indicates 'prefix'. If it appears in [], it indicates 'negative 'grep' ^ [^ Bb] .. 'aaa.txt -- color # search for lines that do not start with B or B and are followed by two characters from aaa.txt.
8. \ <match the start of a word
Grep '\ <Tom 'aaa.txt -- color # search for the line starting with Tom from the aaa.txt file.
9. \> end of matching word
Grep '\> Tom 'aaa.txt -- color # search for the line whose words end with Tom from the aaa.txt file.
10. match a word, not a part of a string
Grep '\ bTom \ B' aaa.txt -- color # searches for lines containing the word Tom from the aaa.txt file. If it is aTomb, it is ignored.
11. Use \ (\) as the label, and use \ n (n is a number) to reference it later. \ 1 indicates that the first one is caused.
Grep '\ (tom \)... \ 1 'aaa.txt -- color # search for tom from the file aaa.txt, and it contains three characters, followed by a tom line. example: tomxxxtom
12. \ {n \} indicates that the previous character must appear n times
Grep 'tomx \ {3 \} 'aaa.txt -- color # search for tomx from the file aaa.txt. x appears three times, for example, tomxxx grep 'tomx \ {3, \} 'aaa.txt -- color # search for tomx from aaa.txt. x must appear at least three times, for example, tomxxx or tomxxxx grep 'tomx \ {3, 4 \} 'aaa.txt -- color # search for tomx from aaa.txt, x appears 3 or 4 times, such as tomxxx or tomxxxx