shell脚本快速入门之-----正则三剑客之一grep用法大全!!!

元字符总结

  • ^ 匹配输入字符串的开始位置。除非在方括号表达式中使用,表示不包含该字符集合。要匹配“^” 字符本身,请使用“^”

  • $ 匹配输入字符串的结尾位置。如果设置了RegExp 对象的 Multiline 属性,则“KaTeX parse error: Undefined control sequence: \n at position 6: ”也匹配‘\̲n̲’或‘\r’。要匹配“”字符本身,请使用“$”匹配除“\r\n”之外的任何单个字符

  • \ 反斜杠,又叫转义字符,去除其后紧跟的元字符或通配符的特殊意义

  • "*※"匹配前面的子表达式零次或多次。要匹配“※”字符,请使用“\※”

  • [] 字符集合。匹配所包含的任意一个字符。例如,“[abc]”可以匹配“plain”中的“a”

  • [^] 赋值字符集合。匹配未包含的一个任意字符。例如,“[^abc]”可以匹配“plain”中任何一个字母

  • [n1-n2] 字符范围。匹配指定范围内的任意一个字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意一个小写字母字符。
    注意:只有连字符(-)在字符组内部,并且出现在两个字符之间时,才能表示字符的范围;如果出现在字符组的开头,则只能表示连字符本身

  • {n} n 是一个非负整数,匹配确定的 n 次。例如,“o{2}”不能匹配“Bob”中的“o”,但是能匹配“food”中的“oo”

  • {n,} n 是一个非负整数,至少匹配 n 次。例如,“o{2,}”不能匹配“Bob”中的“o”,但能匹配“foooood”中的所有o。“o{1,}”等价于“o+”。“o{0,}”则等价于“o*”

  • {n,m} m 和 n 均为非负整数,其中 n<=m,最少匹配 n 次且最多匹配m 次**

  • 加号+ 代表匹配前一个字符一次或者多次

  • 问号? 等同于*号 都是匹配0次或者多次

一、grep用法大全

1、把空行过滤掉然后显示

[root@promote opt]# grep -v '^$' httpd.conf 
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding

2、[ie]表示匹配任意一个

[root@promote opt]# grep -n 'th[ie]' httpd.conf 
2:# This is the main Apache HTTP server configuration file.  It contains the
3:# configuration directives that give the server its instructions.
9:# Do NOT simply read the instructions in here without understanding
10:# what they do.  They're here only as hints or reminders.  If you are unsure

3、查找重复单个字符

[root@promote opt]# grep -n 'oo' httpd.conf 
16:# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
17:# with ServerRoot set to '/www' will be interpreted by the
22:# ServerRoot: The top of the directory tree under which the server's
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
28:# same ServerRoot for multiple httpd daemons, you will need to change at

4、不以w开头的并且后面接oo的过滤出来

[root@promote opt]# grep -n '[^w]oo' httpd.conf 
16:# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
17:# with ServerRoot set to '/www' will be interpreted by the
22:# ServerRoot: The top of the directory tree under which the server's
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
28:# same ServerRoot for multiple httpd daemons, you will need to change at
31:ServerRoot "/etc/httpd"
54:# LoadModule foo_module modules/mod_foo.so
60:# httpd as root initially and it will switch.  
63:# It is usually good practice to create a dedicated user and group for
86:ServerAdmin root@localhost
115:# DocumentRoot: The directory out of which you will serve your
119:DocumentRoot "/var/www/html"
130:# Further relax access to the default document root:
226:    # Redirect permanent /foo http://www.example.com/bar
230:    # access content that does not live under the DocumentRoot.
332:#ErrorDocument 500 "The server made a boo boo."
356:wooo
357:woooo

5、过滤出不以a-z A-Z开头的

[root@promote opt]# grep -v '^[a-zA-Z]' httpd.conf 
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.

^括号在[]外的代表以什么什么开头的 在[]内表示取反 反过滤

6、过滤出0-9和a-z的

[root@promote opt]# grep -n '[0-Z]' httpd.conf 
[root@promote opt]# grep -n '[0-Z]' httpd.conf 
2:# This is the main Apache HTTP server configuration file.  It contains the
3:# configuration directives that give the server its instructions.
4:# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
5:# In particular, see 
6:# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
7:# for a discussion of each configuration directive.
9:# Do NOT simply read the instructions in here without understanding
10:# what they do.  They're here only as hints or reminders.  If you are unsure
11:# consult the online docs. You have been warned.  
13:# Configuration and logfile names: If the filenames you specify for many

7、过滤出以root开头的 以bash结尾的

```handlebars
[root@promote opt]# grep -n '^root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
[root@promote opt]# grep -n 'bash$' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
44:shang:x:1000:1000:shang:/home/shang:/bin/bash

8、过滤出以.结尾的 这里的.需要转义 查找任意一个字符“.”

[root@promote opt]# grep -n '\.$' httpd.conf 
3:# configuration directives that give the server its instructions.
4:# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
7:# for a discussion of each configuration directive.
19:# interpreted as '/log/access_log'.
23:# configuration, error, and log files are kept.

9、筛选出oo或者oo再多几个o的字段 *代表前一个字符的0个或者多个匹配

[root@promote opt]# grep -n 'ooo*' httpd.conf ^C
355:woo
356:wooo
357:woooo

10、查询以 w 开头 d 结尾,中间包含至少一个 o 的字符串

[root@localhost ~]# grep -n 'woo*d' test.txt
8:a wood cross! 11:#woood # 12:#woooooood #

11、查询以 w 开头 d 结尾,中间的字符可有可无的字符串。

[root@localhost ~]# grep -n 'w.*d' test.txt
1:he was short and fat.
5:google is the best tools for search keyword. 8:a wood cross!
9:Actions speak louder than words 11:#woood #
12:#woooooood #

12、查询任意数字所在行

[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt 4:
the tongue is boneless but it breaks bones.12!
 7:PI=3.141592653589793238462643383249901429

13、 grep -n '’ httpd.conf 匹配的字符 不需要转义

14、查询以 w 开头以 d 结尾,中间包含 2 个或 2 个以上 o 的字符串。

[root@localhost ~]# grep -n 'wo\{2,\}d' test.txt
8:a wood cross!
11:#woood # 12:#woooooood #

15、 [root@promote opt]# grep -n ‘o+’ httpd.conf 表示匹配一个o的字符

二、 egrep

1、特点:与grep的区别 使用场景

egrep 当使用多个表达式双重多重过滤
执行“grep -v‘^KaTeX parse error: Expected group after '^' at position 21: ….txt | grep -v‘^̲#’”即可实现。这里需要使用管…|^#’test.txt”,其中,单引号内的管道符号表示或者(or)。

2、 +号的使用方法不同与※号不通之处在于

'+ '作用:重复一个或者一个以上的前一个字符
与※不同的是 ※可以匹配0次而+不能

(1)egrep 直接用’wo+’

[root@promote opt]# egrep -n 'wo+' httpd.conf 
109:# particular features to be enabled - so if something's not working as
148:    # It can be "All", "None", or any combination of the keywords:
342:# be turned off when serving from networked-mounted 
354:wo
355:woo
356:wooo
357:woooo

(2)grep想要实现+的功能还要加上转义符\

[root@promote opt]# grep -n 'wo\+' httpd.conf 
109:# particular features to be enabled - so if something's not working as
148:    # It can be "All", "None", or any combination of the keywords:
342:# be turned off when serving from networked-mounted 
354:wo
355:woo
356:wooo
357:woooo

(3)想要达到同样的需求也可以用’?'同样的grep需要加转义符 egrep不需要 在这里跟※一样

[root@promote opt]# grep -n 'wo\?' httpd.conf 
343:# filesystems or if support for these functions is otherwise
354:wo
355:woo
356:wooo
357:woooo
egrep -n 'wo?' httpd.conf 
343:# filesystems or if support for these functions is otherwise
354:wo
355:woo
356:wooo
357:woooo

3、 ‘+’ ‘*’ ‘?’ {}总结

‘*’ 和‘?’一样 都是匹配前一个字符0或者多次
‘+’ 是匹配前一个字符 一次或者多次 等同于大括号 里面一次
这里还是大括号最精确 匹配指定次数

[root@promote opt]# grep -n 'wo\{1,\}' httpd.conf 
109:# particular features to be enabled - so if something's not working as
148:    # It can be "All", "None", or any combination of the keywords:
342:# be turned off when serving from networked-mounted 
354:wo
355:woo
356:wooo
357:woooo

4、 egrep 总结

grep的所有功能全部都能实现 并且使用特殊字符 + ? 不用加转义
两次过滤直接用管道符过滤就可以

猜你喜欢

转载自blog.csdn.net/weixin_47219935/article/details/107574485