Linux grep is a command that matches and searches on text and content based on rows.
The grep command syntax and common option are as follows:
Usage: grep [options] ... pattern [FILE] ...
Find pattern in each FILE or standard input.
The default pattern is a basic regular expression (abbreviated as BRE).
For example: Grep-i ' Hello World ' menu.h main.c
--color: Coloring a matching string to print
-O: Displays only what is matched to the pattern
-C: Print out the number of rows that match to the string
-i:ignore case, does not distinguish between character case
-V: Shows rows that cannot be matched by a pattern
-E: Using extended regular expressions
-N: Prints the line number in the file that matches the string
-R,-r: recursively each file in all folders
-F: Get pattern from FILE
Stop-m:num after a second match
-L: Print out file names that contain matching strings, not matching content
-L: Prints a file name that does not contain a matching string, as opposed to the-l option
-H: Print out the file name of the string for each matching string
-H: Do not print filename of file that matches string
-W: Force pattern to match only words exactly
-X: Force pattern to match only one row exactly
-A N: Print matching strings while printing the first N rows of all rows matching the string
-B N: Print matching strings while printing the following N lines of the line that matches the string
-C N: Print matching string at the same time print matching string of rows of the first n rows and after n lines
--exclude=glob: Skip GLOB matching filename for search
--exclude-dir=dir: Skips the folder specified by Dir in recursive search
grep common usage:
(1) Recursively search for files in the current directory and display only file names
grep "Redis". -r-l
(2) Search for the specified string and display the line number
cat/etc/passwd | Grep-in "Mysql"
(3) Read the keyword from the specified file to search
root@mckee-pc:~# Cat Test.txt
Linux
Peida.cnblogs.com
Ubuntu
Ubuntu Linux
Redhat
Redhat
LinuxMint
root@mckee-pc:~# Cat Test2.txt
Linux
Redhat
root@mckee-pc:~# Cat Test.txt | Grep-f Test2.txt
Linux
Ubuntu Linux
Redhat
LinuxMint
(4) matches a row at the end of "at"
Cat Test.txt | Grep-e "at$"
(5) Matches only words and displays the number of matching rows
Grep-w-C "usr"/etc/passw