Grammar
# grep Match_pattern filename//will output content that complies with the Match_pattern rules, match pattern is a wildcard character
# grep-e Match_pattern filename//The match_pattern here is a regular expression
# grep-o-e match_pattern filename//output only matching parts
# grep-v Match_pattern filename//contains rows outside the line that Math_pattern matches
# grep-c "text" FileName//Match string number of lines
# Egrep-o Match_pattern | Wc-l//Number of occurrences of a string
# grep "Text". -r-n//Recursive search file
# GREP-E MATCH_PATTERN-E match_pattern//Match multiple styles
# grep-f Pattern_file source_filename//Match multiple styles, styles appear as files
# grep match_pattern-a 3//3 lines after a result
# grep Match_pattern-b 3//3 lines before a result
# grep match_pattern-c 3//front and back 3 rows of a result
Apply the line that appears in the string "Linux" in the search file
# grep Linux-n sample.txt
1:GNU is not Linux
2:linux is fun
Search for multiple file strings "Linux" lines that appear
# grep Linux-n sample.txt sample2.txt
SAMPLE.TXT:1:GNU is not Linux
Sample.txt:2:linux is fun
Sample2.txt:4:planetlinux
Search for string "is" using regular expressions
# egrep "[i|s]+" Sample.txt
GNU is not Linux
Linux is fun
Bash is art
Calculates the position of the matching string not appearing throughout the file
# egrep "[i|s]+"-b-o sample.txt
4:is
23:is
35:is
Search multiple files and find out which file the matching text is in
# grep-l Linux sample.txt sample2.txt
Sample.txt
Sample2.txt
Search for files that do not contain matching text
# grep-l Linux sample.txt sample2.txt
Search files recursively
# grep "Linux". -r-n
./sample2.txt:4:planetlinux
./data.txt:4:4 Linux 1000
./NEWDIR/SAMPLE.TXT:1:GNU is not Linux
./newdir/sample.txt:2:linux is Fun
./SAMPLE.TXT:1:GNU is not Linux
./sample.txt:2:linux is Fun
Ignore case when searching for files
# grep-i "LINUX" sample.txt
GNU is not Linux
Linux is fun
Match multiple styles
# GREP-E "Gun"-E "Linux" Sample.txt
GNU is not Linux
Linux is fun
Include or exclude files in your search
# grep "Hello". -R--include *.txt
Out.txt:hello
# grep "Hello". -R--exclude "*.txt"
./test:hello
Whether the file contains the specified text
#!/bin/Bashif[$#-ne2 ]; ThenEcho "$ match_text filename"fiMatch_text=$1filename=$2grep-q $match _text $filenameif[$?-eq0 ]; Then Echo "The text exsit"ElseEcho "text doesn ' t exsit"fi
#./silent_grep.sh Student Stu_data.txt
The text exsit
The powerful grep