Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the Linux system grep command

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the Linux system grep command is useful, the article introduces in great detail, has a certain reference value, interested friends must read it!

In Linux system, the full name of grep is Global Regular Expression Print, which represents the global regular expression version. It is a powerful text search tool that can use regular expressions to search text and print matching lines.

Common usage of grep [root@www ~] # grep [- acinv] [--color=auto] 'search string' filename options and parameters:-a: search the binary file as an text file to search for data-c: calculate the number of times to find the 'search string'-I: ignore the case difference, so the case is treated as the same-n: incidentally output line number-v: reverse selection That is, the line that shows no 'search string' content! -- color=auto: you can add the color display to the keywords you find. Take out the line / etc/passwd where there is root.

# grep root / etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin or # cat / etc/passwd | grep root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin takes out the lines with root in / etc/passwd, and displays the line numbers of these lines in / etc/passwd

# grep-n root / etc/passwd 1:root:x:0:0:root:/root:/bin/bash 30:operator:x:11:0:operator:/root:/sbin/nologin in terms of keyword display, grep can use-color=auto to display the keyword part in color. This is a very good feature! But if you have to add your own color=auto every time you use grep, it seems troublesome ~ at this time, the good alias will have to deal with it! You can add this line to ~ / .bashrc: "alias grep='grep-color=auto'" and then "source ~ / .bashrc" to take effect immediately! In this way, every time he runs grep, he will automatically add a color display for you.

Take / etc/passwd, take out the lines that do not have root

# grep-v root / etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin will / etc/passwd, take out the lines without root and nologin

# grep-v root / etc/passwd | grep-v nologin root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin lists the core information using dmesg, and then uses grep to find the line containing eth. Color the captured keywords and add the line number to indicate:

[root@www ~] # dmesg | grep-n-- color=auto 'eth' 247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10 248:eth0: Identified 8139 chip type' RTL-8139C' 294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1 305:eth0: no IPv6 routers present # you will find that in addition to eth will have a special color to represent, there is a line number at the front! In terms of keyword display, grep can use-color=auto to display the keyword part in color. This is a very good feature! But if you have to add your own color=auto every time you use grep, it seems troublesome ~ at this time, the good alias will have to deal with it! You can add this line to ~ / .bashrc: "alias grep='grep-color=auto'" and then "source ~ / .bashrc" to take effect immediately! In this way, every time he runs grep, he will automatically add a color display for you.

Use dmesg to list the core information, and then use grep to find out the line containing eth, and display it in the first two lines and the last three lines of the keyword.

[root@www ~] # dmesg | grep-n-A3-B2-- color=auto 'eth' 245-PCI: setting IRQ 10 as level-triggered 246-ACPI: PCI Interrupt 0000:00:0e.0 [A]-> Link [LNKB]... 247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84 IRQ 10 248:eth0: Identified 8139 chip type 'RTL-8139C' 249-input: PC Speaker as / class/input/input2 250-ACPI: PCI Interrupt 0000Rod 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 lines of keyword 247 and the last three lines of 248 are also displayed! # this allows you to capture the data before and after keywords for analysis! Recursively find the directory according to the contents of the file

# grep 'energywise' * # search for files with' energywise' lines in the current directory # grep-r 'energywise' * # search for files on' energywise' lines in the current directory and its subdirectories # grep-l-r 'energywise' * # search for files on the' energywise' lines in the current directory and its subdirectories, but do not show matching lines, only matching files are shown these commands are very useful Is a sharp weapon to find files.

Grep and regular expression character classes

Character search: if I want to search for the words test or taste, I can find that they have a common 'tactist'. At this time, I can search like this:

[root@www] # grep-n't [ae] st' regular_express.txt 8 st' regular_express.txt I can't finish the test. 9:Oh! The soup taste good. In fact, no matter how many bytes there are in [], he represents a "one" byte, so the above example shows that the string I need is just a "tast" or "test" string!

Reverse selection of character classes [^]: if you want to search for lines with oo, but do not want oo to be preceded by g, as follows

[root@www] # grep-n'[^ g] oo' regular_express.txt 2: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! There is no doubt on line 2 and 3, because both foo and Foo are acceptable!

But line 18 clearly has the goo of google ~ Don't forget, because there is the too of tool behind the line! So the line is also listed ~ that is to say, although there are items we don't want (goo) in the 18 lines, because of the needed items (too), it is in line with the string search!

As for line 19, again, because the oo in goooooogle may be preceded by o, for example: go (ooo) oogle, so this line also meets the requirements!

Character class continuity: again, suppose I don't want lowercase bytes in front of my oo, so I can write abcd like this. .zoo, but this doesn't seem convenient, because the coding order on the lowercase byte ASCII is contiguous, so we can simplify it like this:

[root@www] # grep-n'[^ a Murz] oo' regular_express.txt 3:Football game is not use feet only. That is to say, when we are in a set of bytes, if the byte group is contiguous, such as uppercase / lowercase English / numbers, etc., we can use [Amurz], [Amurz], [0-9] and other ways to write. What if our required strings are numbers and English? He he! Write it all together and turn it into: [a-zA-Z0-9].

We're going to get the line with numbers, that's all:

[root@www] # grep-n'[0-9] 'regular_express.txt 5:However, this dress is about $3183 dollars. 15:You are the best is mean you are the no. 1. Line beginning and end byte ^ $first line character: what if I want the to be listed only at the beginning of the line? At this time, you have to use location bytes! We can do this:

[root@www] # grep-n'^ the' regular_express.txt 12:the symbol'*'is represented as start. At this point, only line 12 is left, because only line 12 begins with the ~ besides, what if I want the line that begins with lowercase bytes to be listed? You can do this:

[root@www] # grep-n'^ [a Murz] 'regular_express.txt 2: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 don't want to start with an English letter, it can be like this:

[root@www ~] # grep-n'^ [^ a-zA-Z] 'regular_express.txt 1: "Open Source" is a good mechanism to develop programs. The 21R # I am VBird ^ symbol is different inside and outside the character class symbol (parenthesis [])! It stands for "reverse selection" in [] and the meaning of positioning at the beginning of the line outside []!

So if I want to find out, the end of the line ends with a decimal point. The line of business:

[root@www] # grep-n'\. $'regular_express.txt 1: "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 worldis the same with "glad". 17:I like dog. 18:google is the best tools for search keyword. 20:go! Go! Let's go. In particular, note that because the decimal point has other meanings (described below), you must use the escape character () to remove its special meaning!

Find the blank line:

[root@www ~] # grep-n'^ $'regular_express.txt 22: because there is only the beginning of the line followed by the end of the line (^ $), so you can find the blank line!

Any byte. The meanings of the symbols and duplicate bytes * in regular expressions are as follows:

. (decimal point): it means "there must be an arbitrary byte"; * (asterisk): it means "repeat the previous character, 0 to infinitely many times". In order to combine the shape, suppose I need to find the string of gcharacters, that is, a total of four bytes, starting with g and ending with d. I can do this:

[root@www] # grep-n'g.. d' regular_express.txt 1: "Open Source" is a good mechanism to develop programs. 9:Oh! The soup taste good. 16:The worldis the same with "glad". Because it is emphasized that there must be two bytes between g and d, the god on line 13 and the gd on line 14 will not be listed!

What if I want to list data with oo, ooo, oooo, etc., that is, at least two (inclusive) o or more?

Because * represents the meaning of "repeating 0 or more previous RE characters", "o" means "having a null byte or more than one o byte", so "grep-n 'o' regular_express.txt" will print all the data on the terminal!

When we need "at least two o strings", we need ooo*, that is:

[root@www] # grep-n 'ooo*' regular_express.txt 1: "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 both the beginning and the end of the string to be g, but there can be at least one o between the two g, that is, gog, goog, gooog... . Wait, what then?

[root@www] # grep-n 'goo*g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes! If I want to find out the lines that begin with and end with g, the characters in it are optional.

[root@www] # grep-n 'g.roomg' regular_express.txt 1: "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 of g and the end of g, any byte in the middle is acceptable, so lines 1, 14, and 20 are acceptable! It is common for the RE of this. * to represent any character.

What if I want to find out the line of "any number"? Because there are only numbers, it becomes:

[root@www] # grep-n'[0-9] [0-9] * 'regular_express.txt 5:However, this dress is about $3183 dollars. 15:You are the best is mean you are the no. 1. Limit the range of consecutive RE characters {} we can take advantage of. Configure 0 to infinitely many repeating bytes with RE characters and *, what if I want to limit the number of repeating bytes in a range?

For example, I want to find two to five consecutive strings of o, what should I do? At this point, you have to use a limited range of characters {}. But because the symbol of {and} has a special meaning in shell, we have to use the character\ to make it lose its special meaning. As for the syntax of {}, suppose I want to find two strings of o, which can be:

[root@www ~] # grep-n'o\ {2\} 'regular_express.txt 1: "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 ke 19:goooooogle yes! Suppose we want to find out that g is followed by two or five os, and then a string of g, he would look like this:

[root@www] # grep-n'go\ {2pr. 5\} g 'regular_express.txt 18:google is the best tools for search keyword. If what I want is more than 2 o goooo... Where is .g? In addition to being gooo*g, it can also be:

[root@www] # grep-n'go\ {2,\} g 'regular_express.txt 18: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 lines that contain NW or EA. If you do not use egrep, but grep, there will be no results found.

# egrep'NW | EA' testfile northwest NW Charles Main 3.0. 98 3 34 eastern EA TB Savage 4.4. 84 520 for standard grep, if you precede the extended metacharacter with\, grep automatically enables the extension option-E.

# grep'NW\ | EA' testfile northwest NW Charles Main 3.0. 98 3 34 eastern EA TB Savage 4.4. 84 520 searches for all rows containing one or more 3s.

# egrep'3 commands' testfile # grep-E'3 commands' testfile # grep'3\ + 'testfile # will northwest NW Charles Main 3.098 334 western WE Sharon Gray 5.3.97 523 northeast NE AM Main Jr. 5.94 3 13 central CT Ann Stephens 5.794 5 13 searches for all lines that contain 0 or 1 decimal character.

# egrep'2\. [0-9] 'testfile # grep-E' 2\. [0-9] 'testfile # grep' 2\.\? [0-9] 'testfile # first contains 2 characters, followed by 0 or 1 dot, followed by a number between 0 and 9. Western WE Sharon Gray 5. 3. 97 5 23 southwest SW Lewis Dalsass 2. 8 2 18 eastern EA TB Savage 4. 84 5 20 searches for rows of one or more consecutive no

# egrep'(no) + 'testfile # grep-E' (no) + 'testfile # grep'\ (no\)\ + 'testfile # three commands return the same result, northwest NW Charles Main 3.098 334 northeast NE AM Main Jr. 5.1. 94 3 13 north NO Margot Weber 4.589 59 9 does not use regular expressions

Fgrep queries are faster than the grep command, but not flexible: it can only find fixed text, not regular expressions.

If you want to find a line containing an asterisk character in a file or output

Fgrep'*'/ etc/profile for i in / etc/profile.d/*.sh; do or grep-F'*'/ etc/profile for i in / etc/profile.d/*.sh; do is all the content of this article "what is the use of grep commands in Linux systems?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report