[Linux study notes 28-1] Detailed explanation of the grep command of the Shell text processing tool

grep (global search regular expression(RE) and print out the line, search for regular expressions and print out the line) is a powerful text search tool that can search for text using regular expressions and print out the matching lines come out


1. Detailed explanation of grep command parameters


Command format:grep 匹配条件 处理文件

grep -E = egrep	#命令相同

-i
Ignore case filtering
-E "\<text"
No characters before the filter text
-E "text\>"
No characters after the filter text
-digital
Show the filter line and the upper and lower lines
-n
Display the line number of the matched line
-B
Show the filter line and the lines above
-A
Show the filter line and the following lines
-v
Reverse filter
-O
Only output the matched part of the file

1.1 grep command usage example


1 Filter multiple texts at the same time

Insert picture description here

2 Ignore case filter

Insert picture description here

3 Specify the text as the beginning or end to filter

Insert picture description here

4 Display the line number of the matched line

Insert picture description here

5 Display the filter line and the upper or lower lines

Insert picture description here

6 reverse filtering

Insert picture description here

2. Grep character number matching rules


^Text
Start with this text
Text$
End with this text
w..s
Start with w and end with 2 arbitrary characters in the middle
....s
End with s, any 5 characters in front
*
Any character
0-1 times
+
1- any time
{n}
n times
{m,n}
mn times
{,n}
0-n times
{m,}
At least m times
(Text){times}
How many times the text appears

2.1 Example of grep character limit


1 Specify the beginning or end of the text

Insert picture description here

2 Specify any characters apart

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

3. Practice script: display the names of users in the system that can be switched by the su command


grep -E "bash$|sh$" /etc/passwd | cut -d : -f 1

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46069582/article/details/111129938