一.grep简介

grep 全程Globally search a Regular Expression and Print,是一种强大的文本搜索工具,它能使用特 定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行。Unix的grep家族包括grep和egrep

二.grep的工作模式

2.1.grep执行语法

grep [option] file...

2.2.工作方式

grep在一个或者多个文件中搜索字符串模板

如果模板中包括空格,需要使用引号引起来

模板后的所有字符串会被看作是文件名

2.3.工作结果

如果模板搜索成功,则返回0状态码

如果搜索不成功,则返回1状态码

如果搜索的文件不存在,则返回2的状态码。

三.grep的常用参数详解

3.1.常用参数详解

3.2.演示示例

a)建立测试文件

[root@qingdeng shell]#  vim testfile

b)过滤文件中所有含有root字符串的行

[root@qingdeng shell]# grep root testfile
 root:x:0:0:root:/root:/bin/bash
 operator:x:11:0:operator:/root:/sbin/nologin
 test:root:test
 test:chroot:test
 test:test:root

c)过滤包含root单词的行

[root@qingdeng shell]#  grep -w root testfile
 root:x:0:0:root:/root:/bin/bash
 operator:x:11:0:operator:/root:/sbin/nologin
 test:root:test
 test:test:root

d)过滤含有root字符的行忽略大小写

[root@qingdeng shell]#  grep -i root testfile
 root:x:0:0:root:/root:/bin/bash
 operator:x:11:0:operator:/root:/sbin/nologin
 test:root:test
 test:ROOT:test
 test:chroot:test
 test:test:root

e)过滤含有root单词和bash单词的行

[root@qingdeng shell]#  grep -w -e root -e bash testfile
 root:x:0:0:root:/root:/bin/bash
 operator:x:11:0:operator:/root:/sbin/nologin
 test:root:test
 test:test:root

f)只显示匹配内容不不显示其他并标注行号

[root@qingdeng shell]#  grep -on root testfile
1:root
1:root
1:root
10:root
11:root
13:root
14:root

g)显示包含root字符串的行的总数

[root@qingdeng shell]#  grep -c root testfile
5

h)显示不含有root的行

[root@qingdeng shell]#  grep -v root testfile
 bin:x:1:1:bin:/bin:/sbin/nologin
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 adm:x:3:4:adm:/var/adm:/sbin/nologin
 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 sync:x:5:0:sync:/sbin:/bin/sync
 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
 halt:x:7:0:halt:/sbin:/sbin/halt
 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
 test:ROOT:test

j)显示含有adm关键字及其周围的2行

[root@qingdeng shell]#  grep -2 adm testfile
 bin:x:1:1:bin:/bin:/sbin/nologin
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 adm:x:3:4:adm:/var/adm:/sbin/nologin
 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 sync:x:5:0:sync:/sbin:/bin/sync

k)显示含有adm关键字及其下面1行

[root@qingdeng shell]#  grep -A1 adm  testfile
 adm:x:3:4:adm:/var/adm:/sbin/nologin
 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

l)显示含有adm关键字及其上面2行

[root@qingdeng shell]#  grep -B2 adm  testfile
 bin:x:1:1:bin:/bin:/sbin/nologin
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 adm:x:3:4:adm:/var/adm:/sbin/nologin

四.正则表达式的使用

4.1.什么是正则表达式

正则表达式就是用在字符串的处理上面的一项表示式 在我们做程序设计时很多需要对一类字符进行处理,正则表达式就完美的解决了这个问题

4.2.正则表达式

基本正则表达式(Basic Regular Expression,BRE),又称为标准正则表达式,是最早制订的正则表达 式规范,仅支持最基本的元字符集。基本正则表达式是POSIX规范制订的两种正则表达式语法标准之一, 另外一种语法标准称为扩展正则表达式

扩展正则表达式(Extended Regular Expression,ERE)支持比基本正则表达式更多的元字符

示例:

a)过滤以root开头的行,过滤以root开头bash结尾的行

[root@qingdeng shell]# grep -E "(^root|bash$)" testfile
 root:x:0:0:root:/root:/bin/bash
[root@qingdeng shell]# grep ^root testfile

b)过滤以root结尾的行

[root@qingdeng shell]#  grep root$ testfile
 test:test:root

c)搜索e开头y结尾的三字符单词,搜索e开头y结尾的四字符单词

[root@qingdeng shell]# vim testfile1

[root@qingdeng shell]#  grep e.y testfile1
eay
eby
[root@qingdeng shell]#  grep e..y testfile1
eaay
ebby
eaby

d)搜索e开头Y结尾的所有行

[root@qingdeng shell]# grep  e.*y testfile1
eay
eby
eaay
ebby
eaaay
ebbby
eaaaaay
ebbbbby
eababy
eaby
eabababy

e)搜索自定次数出现的字符

[root@qingdeng shell]#  grep -E  "e.{2}y" testfile1
eaay
ebby
eaby
[root@qingdeng shell]#  grep -E  "e.{,2}y" testfile1
eay
eby
eaay
ebby
eaby
[root@qingdeng shell]#  grep -E  "e.{2,}y" testfile1
eaay
ebby
eaaay
ebbby
eaaaaay
ebbbbby
eababy
eaby
eabababy
[root@qingdeng shell]#  grep -E  "e.{2,3}y" testfile1
eaay
ebby
eaaay
ebbby
eaby
[root@qingdeng shell]#  grep -E  "e.?y" testfile1
eay
eby
[root@qingdeng shell]#  grep -E  "e.+y" testfile1
eay
eby
eaay
ebby
eaaay
ebbby
eaaaaay
ebbbbby
eababy
eaby
eabababy
[root@qingdeng shell]#  grep -E  "e[^ac]?y" testfile1
eby
[root@qingdeng shell]# grep -E  "e[ac]?y" testfile1
eay
[root@qingdeng shell]#  grep -E  "e(ab)?y" testfile1
eaby

4.3.正则表达式字符集

 [root@qingdeng shell]# touch  easyalee easyAlee "easy lee"  "easy@lee" 
"easy8lee"
 [root@qingdeng shell]# rm -fr easy[[:alpha:]]lee
 [root@qingdeng shell]# rm -fr easy[[:lower:]]lee
 [root@qingdeng shell]# rm -fr easy[[:upper:]]lee
 [root@qingdeng shell]# rm -fr easy[[:digit:]]lee
 [root@qingdeng shell]# rm -fr easy[[:space:]]lee
 [root@qingdeng shell]# rm -fr easy[[:graph:]]lee

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐