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

时间:2022-07-24
本文章向大家介绍shell脚本快速入门之-----正则三剑客之一grep用法大全!!!,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

元字符总结

  • ^ 匹配输入字符串的开始位置。除非在方括号表达式中使用,表示不包含该字符集合。要匹配“^” 字符本身,请使用“^”
  • $ 匹配输入字符串的结尾位置。如果设置了RegExp 对象的 Multiline 属性,则“KaTeX parse error: Undefined control sequence: n at position 6: ”也匹配‘̲n̲’或‘r’。要匹配“”字符本身,请使用“$”匹配除“rn”之外的任何单个字符
  • 反斜杠,又叫转义字符,去除其后紧跟的元字符或通配符的特殊意义
  • "*※"匹配前面的子表达式零次或多次。要匹配“※”字符,请使用“※”
  • [] 字符集合。匹配所包含的任意一个字符。例如,“[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 
:# This is the main Apache HTTP server configuration file.  It contains the
:# configuration directives that give the server its instructions.
:# Do NOT simply read the instructions in here without understanding
:# what they do.  They're here only as hints or reminders.  If you are unsure

3、查找重复单个字符

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

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

[root@promote opt]# grep -n '[^w]oo' httpd.conf 
:# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
:# with ServerRoot set to '/www' will be interpreted by the
:# ServerRoot: The top of the directory tree under which the server's
:# ServerRoot at a non-local disk, be sure to specify a local disk on the
:# same ServerRoot for multiple httpd daemons, you will need to change at
:ServerRoot "/etc/httpd"
:# LoadModule foo_module modules/mod_foo.so
:# httpd as root initially and it will switch.  
:# It is usually good practice to create a dedicated user and group for
:ServerAdmin root@localhost
:# DocumentRoot: The directory out of which you will serve your
:DocumentRoot "/var/www/html"
:# Further relax access to the default document root:
:    # Redirect permanent /foo http://www.example.com/bar
:    # access content that does not live under the DocumentRoot.
:#ErrorDocument  "The server made a boo boo."
:wooo
: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 
:# 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
:# what they do.  They're here only as hints or reminders.  If you are unsure
:# consult the online docs. You have been warned.  
:# Configuration and logfile names: If the filenames you specify for many

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

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

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

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

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

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

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

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

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

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

12、查询任意数字所在行

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

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

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

[root@localhost ~]# grep -n 'wo{2,}d' test.txt
:a wood cross!
:#woood # :#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 
:# particular features to be enabled - so if something's not working as
:    # It can be "All", "None", or any combination of the keywords:
:# be turned off when serving from networked-mounted 
:wo
:woo
:wooo
:woooo

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

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

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

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

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

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

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

4、 egrep 总结

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