Introduction
In fact, this kind of demand is still used a lot in work. For example, when you are migrating a database, you have to know which projects use the specified database. It is still very time-consuming to view the configuration files one by one.
There are 2 ways of writing that I think of
The first way of writing
find /etc -type f | xargs grep -l 'abc'
The second way of writing
grep -rl abc /etc
The find command is quite powerful. I plan to reopen an article and share it. Today I will share the use of grep egrep
usage
There are generally two forms of use as follows
The first form is
grep [option] [pattern] [file1,file2]
For example, find the line containing content in show.txt
grep content show.txt
The second form
command | grep [option] [pattern]
Such as viewing a certain service information
ps -ef | grep mongo
For example, find the line containing content in show.txt
cat show.txt | grep content
Must master options
Options | meaning |
---|---|
-v | Display mismatched line information (reverse search) |
-i | Ignore case when searching |
-n | Show line number (line number in file) |
-r | Recursive search (search folders) |
-E | Support extended regular expression |
-F | Do not match according to regular expression, match according to the string literal meaning |
The content in the show.txt file is as follows
a
b
c
d
py*
i love python
-v option
grep -v a show.txt
b
c
d
*py
i love python
-n option
grep -n a show.txt
1:a
-r option
Find /etc/myconfig and its subdirectories, and print out the content of the line containing the content string
grep -r content /etc/myconfig
-F option
grep py* show.txt
py*
i love python
py is treated as a regular expression, I want to search the content of py, and I can use the -F option
grep -F py* show.txt
py*
Learned options
Options | meaning |
---|---|
-c | Only output the number of matching rows without displaying specific content |
-w | Match whole word |
-x | Match the entire line |
-l | Only list the matching file names, not the specific matching line content |
-a | Convert binary files to text |
The file content of show.txt is as follows
love
lovelove
i love
i love a
-w option (the word lovelove is not displayed, because there must be spaces or tabs before and after love to be considered a word)
grep -w love show.txt
love
i love
i love a
-x option (match line, the content of the line can only be i love a)
grep -x "i love a" show.txt
i love a
These options can be mixed, for example,
search /etc/myconfig and its subdirectories, and print out the file name of the file containing the content string
grep -rl mad81 /etc/myconfig
-r: Recursive search (search for folders)
-l: Only list the matching file names, and do not display the specific matching line content,
Of course, find can also be used, but it is a little troublesome
find /etc/myconfig -type f | xargs grep -l 'abc'
Common options for viewing logs
Options | meaning |
---|---|
-C n | Show matching line and 5 lines before and after it |
-B n | Show matching rows and the first 5 rows |
-A n | Display the matching line and the next 5 lines |
The content of show.txt is as follows
1
2
3
4
5
6
7
Find 4 and its upper and lower 2 lines
cat show.txt | grep -C 2 4
2
3
4
5
6
Find 4 and the first 2 lines
grep -B 2 4 show.txt
2
3
4
The difference and connection between grep and egrep
Grep does not support extended regular expressions by default, only basic regular expressions.
Use grep -E to support extended regular expressions.
Use egrep to support extended regular expressions, which is equivalent to grep -E