Common CentOS command ---- grep command
Introduction
Grep (global search regular expression (RE) and print out the line, full search for regular expressions and print out rows) is a powerful text search tool, it can use regular expressions to search for text and print matching rows.
Unix grep families include grep, egrep, and fgrep. The commands of egrep and fgrep are only slightly different from those of grep. Egrep is an extension of grep and supports more re metacharacters. fgrep is fixed grep or fast grep. They regard all the letters as words, that is, the metacharacters in a regular expression represent the literal meaning of the regular expression. They are no longer special. Linux uses GNU grep. It is more powerful and can use egrep and fgrep functions through the-G,-E,-F command line options.
Common grep usage
[Root @ www ~] # Grep [-acinv] [-- color = auto] 'search for string' filename options and parameters:-a: Search for data from binary files in text format-c: calculate the number of times the 'searchstring' is found-I: The Case sensitivity is ignored, so the case sensitivity is treated as the same-n: By the way, the output line number-v: reverse selection, that is, the line without 'search string' is displayed! -- Color = auto: You can add the color of the keywords to the display!
Extract the/etc/passwd with the root line
# Grep root/etc/passwdroot: x: 0: 0: root:/bin/bashoperator: x: 11: 0: operator:/root: /sbin/nologin or # cat/etc/passwd | grep root: x: 0: 0: root:/bin/bashoperator: x: 11: 0: operator: /root:/sbin/nologin
Extract the/etc/passwd from the root row, and display the row numbers in/etc/passwd.
# grep -n root /etc/passwd1:root:x:0:0:root:/root:/bin/bash30:operator:x:11:0:operator:/root:/sbin/nologin
For keyword display, you can use -- color = auto to display the keywords in color. This is a very good feature! However, if you use grep each time, you have to add -- color = auto again, which is very troublesome ~ Now, the easy-to-use alias has to be processed! You can ~ /. Add this line to bashrc: "alias grep = 'grep -- color = auto'" and then use "source ~ /. Bashrc "to take effect immediately! In this way, the grep will automatically add a color display for you each time you run grep.
Extract the/etc/passwd without the root line
# grep -v root /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin
Extract the rows with no root or nologin from/etc/passwd.
# grep -v root /etc/passwd | grep -v nologinroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin
Use dmesg to list the core information, and then use grep to find the line containing eth. color the captured keywords and add the row number to represent them:
[Root @ www ~] # Dmesg | grep-n -- color = auto 'eth '247: eth0: RealTek RTL8139 at 0xee846000, 00: 90: cc: a6: 34: 84, IRQ 10248: eth0: identified 8139 chip type 'rtl-8139c' 294: eth0: link up, 100 Mbps, full-duplex, lpa 0xC5E1305: eth0: no IPv6 routers present # You will find that in addition to eth, there will be a special color for representation, and there will be a row number at the beginning!
For keyword display, you can use -- color = auto to display the keywords in color. This is a very good feature! However, if you use grep each time, you have to add -- color = auto again, which is very troublesome ~ Now, the easy-to-use alias has to be processed! You can ~ /. Add this line to bashrc: "alias grep = 'grep -- color = auto'" and then use "source ~ /. Bashrc "to take effect immediately! In this way, the grep will automatically add a color display for you each time you run grep.
Use dmesg to list the core information, and use grep to find the row containing eth. The first two rows and the last three rows of the keyword are also displayed.
[Root @ www ~] # Dmesg | grep-n-A3-B2 -- color = auto 'eth '245-PCI: setting IRQ 10 as level-triggered246-ACPI: PCI Interrupt 0000: 00: 0e. 0 [A]-> Link [LNKB]... 247: eth0: RealTek RTL8139 at 0xee846000, 00: 90: cc: a6: 34: 84, IRQ 10248: eth0: Identified 8139 chip type 'rtl-8139c' 249-input: PC Speaker as/class/input/input2250-ACPI: PCI Interrupt :00:01. 4 [B]-> Link [LNKB]... 251-hdb: ATAPI 48X DVD-ROM DVD-R-RAM CD-R/ RW drive, 2048kB Cache, UDMA (66) # As shown above, you will find that the first two and the last three rows of the keyword 247 are also displayed! # This allows you to capture the data before and after keywords for analysis!
Recursively search directories Based on the file content
# Grep 'energywise '* # search for files with the 'energewid' line in the current directory # grep-r 'energewid' * # search for files with the 'energewid' line in the current directory and Its subdirectories
# Grep-l-r 'energywis' * # search for the 'energywis' row files in the current directory and Its subdirectories, but do not display matching lines. Only matching files are displayed.
These commands are very useful and are a powerful tool for searching files.
Grep and Regular Expressions
Character class
Search for character classes: If I want to search for the test or taste words, I can find that they actually have a common't? St' exists ~ In this case, I can search:
[root@www ~]# grep -n 't[ae]st' regular_express.txt8:I can't finish the test.9:Oh! The soup taste good.
In fact, no matter how many bytes are in [], it represents a "one" Byte. Therefore, the above example shows that, I only need two strings: "tast" and "test!
Reverse Selection of character classes [^]: If you want to search for rows with oo, but do not want to have g before oo, as shown below:
[root@www ~]# grep -n '[^g]oo' regular_express.txt2:apple is my favorite food.3:Football game is not use feet only.18:google is the best tools for search keyword.19:goooooogle yes!
Line 2 and 3 have no doubt, because both foo AND Foo can be accepted!
But there are google's goo in line 3 ~ Don't forget, because the too of tool is displayed after this line! So this row is also listed ~ That is to say, although there is a project (goo) we don't want in line 18, it is a string search because there is a required project (too!
As for Row 3, the same is true because the oo in goooogle may be in front of o, for example, go (ooo) oogle. Therefore, this line also meets the requirements!
Consecutive character classes: If I do not want to have lower-case bytes before oo, I can write [^ abcd .... z] oo, but it does not seem very convenient. Since the ASCII encoding sequence of lower-case bytes is continuous, we can simplify it to the following:
[root@www ~]# grep -n '[^a-z]oo' regular_express.txt3:Football game is not use feet only.
That is to say, when we are in a set of bytes, if the byte group is continuous, such as uppercase/lowercase English/numbers, you can use [a-z], [A-Z], [0-9] and other methods to write, then if we require the string is a number and English? Haha! Write all of them together and turn them into: [a-zA-Z0-9].
We need to obtain the line with numbers, just like this:
[root@www ~]# grep -n '[0-9]' regular_express.txt5:However, this dress is about $ 3183 dollars.15:You are the best is mean you are the no. 1.
Byte at the beginning and end of the row ^ $
First line character: What if I want the first line to be listed? At this time, you have to use the location byte! We can do this:
[root@www ~]# grep -n '^the' regular_express.txt12:the symbol '*' is represented as start.
At this point, there are only 12th rows left, because only 12th rows start with the beginning ~ In addition, what if I want to start with a lower-case byte line? You can do this:
[root@www ~]# grep -n '^[a-z]' regular_express.txt2:apple is my favorite food.4:this dress doesn't fit me.10:motorcycle is cheap than car.12:the symbol '*' is represented as start.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.
If I do not want to start with an English letter, it can be like this:
[root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt1:"Open Source" is a good mechanism to develop programs.21:# I am VBird
^ Symbol, which is different from the character symbol! In [], it indicates "reverse selection". In addition to [], it indicates positioning at the beginning of the line!
If I want to find out the line ending with the decimal point:
[root@www ~]# grep -n '\.$' regular_express.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.4:this dress doesn't fit me.10:motorcycle is cheap than car.11:This window is clear.12:the symbol '*' is represented as start.15:You are the best is mean you are the no. 1.16:The world <Happy> is the same with "glad".17:I like dog.18:google is the best tools for search keyword.20:go! go! Let's go.
Note that because the decimal point has other meanings (which will be introduced below), you must use the Escape Character (\) to remove its special meaning!
Find the blank line:
[root@www ~]# grep -n '^$' regular_express.txt22:
Because only the beginning and end of the line (^ $), so you can find a blank line!
Any one byte. It must be the same as the repeated byte *
The meanings of these two symbols in a regular expression are as follows:
. (Decimal point): represents the meaning of "must have any Byte"; * (asterisk): represents the meaning of "repeating the previous character, 0 to infinity multiple times", which is a combination form
Suppose I need to find g ?? The string of d, that is, there are four bytes. the start is g and the end is d. I can do this:
[root@www ~]# grep -n 'g..d' regular_express.txt1:"Open Source" is a good mechanism to develop programs.9:Oh! The soup taste good.16:The world <Happy> is the same with "glad".
It is emphasized that there must be two bytes between g and d. Therefore, the gd of the 13th row and the 14th row will not be listed!
If I want to list data with oo, ooo, oooo, and so on, that is, there must be at least two (inclusive) o, what should I do?
Because * indicates the meaning of "repeating 0 or multiple preceding RE characters", "o *" indicates: "null bytes or more than one o Byte". Therefore, "grep-n 'o * 'regular_express.txt" prints all the data out of the terminal!
When we need "at least two strings of o and above", we need ooo *, that is:
[root@www ~]# grep -n 'ooo*' regular_express.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! The soup taste good.18:google is the best tools for search keyword.19:goooooogle yes!
If I want to start and end a string with g, but there is only one o between the two g, that is, gog, goog, gooog... and so on, what should I do?
[root@www ~]# grep -n 'goo*g' regular_express.txt18:google is the best tools for search keyword.19:goooooogle yes!
If I want to find the rows starting with and ending with g, the characters in the rows can be dispensable.
[root@www ~]# grep -n 'g.*g' regular_express.txt1:"Open Source" is a good mechanism to develop programs.14:The gd software is a library for drafting programs.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.
Because it represents the beginning and end of g, any byte in the middle is acceptable, so 1st, 14, 20 rows are acceptable! This. * RE indicates any character is very common.
If I want to find the line with "any number? Because there are only numbers, it becomes:
[root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt5:However, this dress is about $ 3183 dollars.15:You are the best is mean you are the no. 1.
Limit the continuous RE character range {}
We can use the. And RE characters and * to configure 0 to unlimited repeated bytes. What if I want to limit the number of repeated bytes in a range?
For example, how do I find two to five consecutive o strings? At this time, you have to use the character {} with a limited range. But because the {And} symbols have special significance in shell, we must use the character \ To make it meaningless. The syntax of {} is as follows. Suppose I want to find two o strings, which can be:
[root@www ~]# grep -n 'o\{2\}' regular_express.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! The soup taste good.18:google is the best tools for search ke19:goooooogle yes!
Suppose we want to find 2 to 5 o After g, and then pick up another g string, it will be like this:
[root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt18:google is the best tools for search keyword.
What if I want more than 2 o goooo... g? In addition to gooo * g, it can also be:
[root@www ~]# grep -n 'go\{2,\}g' regular_express.txt18:google is the best tools for search keyword.19:goooooogle yes!
Extended grep (grep-E or egrep ):
The main benefit of using extended grep is the addition of an additional regular expression metacharacter set.
Print all rows that contain NW or EA. If you do not use egrep but grep, no results will be found.
# egrep 'NW|EA' testfile northwest NW Charles Main 3.0 .98 3 34 eastern EA TB Savage 4.4 .84 5 20
For standard grep, if \ is added before the extended metacharacters, grep automatically enables the extended option-E.
#grep 'NW\|EA' testfilenorthwest NW Charles Main 3.0 .98 3 34eastern EA TB Savage 4.4 .84 5 20
Search for all rows that contain one or more three.
# Egrep '3 + 'testfile # grep-E '3 + 'testfile # grep '3 \ + 'testfile # The Three commands will run the northwest NW Charles Main 3.0 command. 98 3 34 western WE Sharon Gray 5.3. 97 5 23 northeast ne am Main Jr. 5.1. 94 3 13 central CT Ann Stephen 5.7. 94 5 13
Search for all rows that contain 0 or 1 decimal point characters.
# Egrep '2 \.? [0-9] 'testfile # grep-e' 2 \.? [0-9] 'testfile # grep' 2 \.\? [0-9] 'testfile # contains 2 characters, followed by 0 or 1 point, followed by a number between 0 and 9. Western WE Sharon Gray 5.3. 97 5 23 southwest SW Lewis Dalsass 2.7. 8 2 18 eastern ea tb Savage 4.4. 84 5 20
Search for one or more consecutive no rows.
# Egrep '(no) + 'testfile # grep-E' (no) + 'testfile # grep' \ (no \) \ + 'testfile #3 commands return the same result, northwest NW Charles Main 3.0. 98 3 34 northeast ne am Main Jr. 5.1. 94 3 13 north NO Margot Weber 4.5. 89 5 9
Do not use regular expressions
The fgrep query speed is faster than the grep command, but is not flexible enough: it can only find fixed text, rather than rule expressions.
If you want to find a line containing the asterisk characters in a file or output
Fgrep '*'/etc/profilefor I in/etc/profile. d /*. sh; do or grep-F '*'/etc/profilefor I in/etc/profile. d /*. sh; do