grep syntax:
grep [option] "string_to_find" filename
Options and parameters:
(1) -i: ignore case search strings
(2) -v: invert, i.e., the output line of text that does not match
(3) -n: number of output lines
(4) -l: Output file name mode can be matched opposite options -L
(5) -q: muting output
May be selected according to the actual needs
string_to_find need to match the pattern, you can fill a string or a regular expression
Filename is the name you want to find files
Existing files test_001.txt, document reads as follows:
yello
hello, hello
hello hello
hello hello
hello hello
hello hello
hello world
1. The statistical document can match the number of rows
grep -c "hello" test_001.txt
Results: 6
2. The statistics file for a matching number of
grep -o "hello" test_001.txt | wc -l
Results: 11
3. recursive search
-r: grep filename parameter can be added when this option means that recursive directory search
columns such as: file test_001.txt of the parent directory is: sj_add
grep -r "the Hello" sj_add
result:
sj_add/test_001.txt:hello,hello
sj_add/test_001.txt:hello,hello
sj_add/test_001.txt:hello,hello
sj_add/test_001.txt:hello,hello
sj_add/test_001.txt:hello,hello
sj_add/test_001.txt:hello world
4. Matching multiple regular expressions: -e: This option plus a regular expression pattern matching is a need
to find matching hello or world line
grep -e "hello" -e "world " test_001.txt
result:
hello, hello
hello hello
hello hello
hello hello
hello hello
hello world
5. Specify / exclude file
--include: Specifies file search
--exclude: exclude files need to be searched
--exclude-dir: exclusion list to be searched
example:
(1) Search sj_add .cpp file and directory .txt yello the rows containing:
. -R & lt grep "yello" TXT ./sj_add --include * {,} CPP
row containing yello (2) sj_add directory search, but does not search the readme file:
grep -R & lt "yello" ./sj_add --exclude "readme"
row with yello's (3) Search sj_add directory, but does not search .git folder:
grep -r "yello" ./sj_add --exclude-dir ".git"