Deep understanding of grep command in Linux



Grep command is a powerful text search tool in Linux system. It can use regular expression to search text and print out the matching lines. The full name of grep is global regular expression print, which means the global regular expression version. Its permission is for all users.

Grep [- acinv] [-- color = auto] 'find string' file name


    – a: The binary file is used to find data in the form of text file

    – c: Count the number of times’ find string ‘is found

    – i: Ignore the difference between case and case, that is, treat case as the same

    – n: Output line number by the way

    – v: Reverse selection, that is, the line without the content of “search string” is displayed

    – l: Only the matching files are displayed, and the specific contents are not displayed

-F < rule file >: specify a rule file whose content contains one or more rule styles, and let grep find the content of the file that meets the rule conditions in the format of one rule style per line.


#Take out the line containing manpath keyword in the file / etc / man.config, and color the keyword found
grep --color=auto MANPATH /etc/man.config

#The output of LS - L contains the content of the letter file (case insensitive)
ls -l | grep -i file

#Find the keyword "Aug" in the file '/ var / log / messages' 
grep Aug /var/log/messages 

#Find the line starting with "Aug" in the file '/ var / log / messages'
grep ^Aug /var/log/messages 

#Select all lines containing numbers in the '/ var / log / messages' file 
grep [0-9] /var/log/messages 

#Search the directory '/ var / log' and subsequent directories for the string "Aug" (recursive search)
grep Aug -R /var/log/* 

#Search the current directory for files with the 'energywise' line
grep 'energywise' *      

#Search the current directory and its subdirectories for files in the 'energywise' line
grep -r 'energywise' *  

#Find the specified process
ps -ef|grep svn

#Read keywords from the file to search, and output the content line of the test.txt file containing the keywords read from the test2.txt file
cat test.txt | grep -f test2.txt

Grep is similar to normal expression

Character search: if I want to search for test or taste, I can find that they have a common “t? ST”. At this time, I can search as follows:

grep -n 't[ae]st' regular_express.txt

As a matter of fact, no matter how many bytes there are in it, it only represents a “one” byte. Therefore, the above example shows that the strings I need are just “tast” or “test”!

Reverse selection of character class [^]: if you want to search for a line with OO, but don’t want g before OO, as follows

grep -n '[^g]oo' regular_express.txt

Continuity of character classes: again, suppose I don’t want lowercase bytes before oo,

grep -n '[^a-z]oo' regular_express.txt

When we are in a set of set bytes, if the byte group is continuous, such as uppercase English / lowercase English / numbers, we can use [A-Z], [A-Z], [0-9] and other methods to write, then what if we require the string to be number and English? ha-ha! He wrote it all together and became [a-za-z0-9].

We want to get the line with the number, and that’s it:

grep -n '[0-9]' regular_express.txt

Start and end bytes ^$

Prefix: what if I want the to be listed only at the beginning of the line? This time we have to use the positioning byte! We can do this:

grep -n '^the' regular_express.txt

What about the line that starts with lowercase bytes? It can be as follows:

grep -n '^[a-z]' regular_express.txt

If I don’t want to start with an English letter, it can be like this:

grep -n '^[^a-zA-Z]' regular_express.txt

^Symbols are different inside and outside character symbols (brackets)! Inside [] stands for “reverse selection”, outside [] stands for the meaning of positioning at the beginning of the line!

The line ending with a decimal point (.)

grep -n '\.$' regular_express.txt

In particular, because the decimal point has other meanings (described below), you must use the escape character () to remove its special meaning!

#Find the blank line
grep -n '^$' regular_express.txt

Any byte. And duplicate bytes*

    . ( Decimal point: it means “there must be an arbitrary byte”;

*(asterisk): stands for “repeat the previous character, 0 to infinite times”. It is a combination form

#We need to find G?? D, that is, there are four bytes, starting with G and ending with D. I can do this:
grep -n 'g..d' regular_express.txt

#Search for at least two o strings, and "O *" stands for "have empty bytes or more than one o byte."
grep -n 'ooo*' regular_express.txt

#We want the beginning and end of the string to be g, but there can only be at least one o between two G, that is, Gog, Google, gooog... And so on
grep -n 'goo*g' regular_express.txt

#Find the lines that start with G and end with G, and the characters are optional
grep -n 'g.*g' regular_express.txt

#Find two o strings, which can be:
grep -n 'o\{2\}' regular_express.txt

#Find a string of G followed by two or five o's and then G's
grep -n 'go\{2,5\}g' regular_express.txt

#Two or more Goooo... G? In addition to goo * g, it can also be goo * G
grep -n 'go\{2,\}g' regular_express.txt

#Find keywords from multiple files
grep 'linux' test.txt test2.txt


The above is all about the grep command in Linux, I hope the content of this article can bring some help to your study or work, if you have any questions, you can leave a message to exchange.

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]