Syntax: grep [-cinvabc] ' word ' filename
-C: Print the number of lines that meet the requirements
-I: Ignore case
-N: Output with the same number of lines as required
-V: Print rows that do not meet the requirements
-A: followed by a number (with or without spaces), for example, –A2 to print the line that meets the requirements and the following two lines
-B: followed by a number, such as –B2, to print the line that meets the requirements and the above two lines
-C: followed by a number, such as –C2, to print the line that meets the requirements and two rows above and below
Examples:
Lines containing ' halt ' and two lines below this line are printed:
Grep-a2 ' Halt '/etc/passwd
The lines containing ' halt ' and the two lines above it are printed:
Grep-b2 ' Halt '/etc/passwd
The lines containing ' halt ' and the lines above and below the line are printed out:
Grep-c2 ' Halt '/etc/passwd
Filter out a line with a keyword and lose a travel number:
Grep-n ' Root '/etc/passwd
Filter lines that do not have a keyword, and enter the travel number:
Grep-nv ' Nologin '/etc/passwd
Filter out all rows that contain numbers:
grep ' [0-9] '/etc/inittab
Filter out all rows that do not contain numbers:
Grep-v ' [0-9] '/etc/inittab
Remove all lines that begin with ' # ':
Grep-v ' ^# '/etc/inittab
Remove all empty lines and lines that begin with ' # ':
Grep-v ' ^# '/etc/crontab |grep-v ' ^$ '
How to print a line that does not start with the English letter
grep ' ^[^a-za-z] ' test.txt
grep ' [^a-za-z] ' test.txt
Filter any character with repeating characters:
grep ' R.. O '/etc/passwd. Represents any one character
grep ' ooo* '/etc/passwd * represents 0 or more preceding characters
grep '. * '/etc/passwd |wc-l '. * ' denotes 0 or more arbitrary characters, and a blank line is included
Specify the number of occurrences of the character to filter:
grep ' o\{2\} '/etc/passwd
Filter one or more of the preceding characters:
Egrep ' o+ ' test.txt
Filter 0 or one of the preceding characters:
Egrep ' O? ' test.txt
Filter string 1 or String 2:
Egrep ' aaa|111|ooo ' test.txt
Applications in Egrep ():
Egrep ' R (oo) | (at) o ' test.txt () denotes a whole, for example (OO) + means 1 ' oo ' Or more ' oo '
Grep/egrep Command collection in Linux