grep命令是支持正则表达式的一个多用途文本搜索工具,grep的一般格式为:
grep命令由选项、模式和文件三部分组成,它在一个或多个文件中搜索满足模式的文本行,模板后的所有字符串被看做文件名,文件名可以有多个,搜索的结果被打印到屏幕,不影响原文件的内容。grep命令的选项用于对搜索过程进行补充说明,grep命令的选项及其意义如表3-3 所示。
表3-3 grep命令选项及其意义
![]() |
- #例3-24:模式包含空格时,是否使用双引号的区别
- #搜索00.pem 文件中包含certificate字符串的行,不需要引号引起模式
- [root@zawu globus]# grep certificate 00.pem
- The above string is known as your user certificate subject, and it
- To install this user certificate, please save this e-mail message
- If you have any questions about the certificate contact
- #若需要搜索00.pem 文件中包含user certificate字符串的行
- #我们看一看不用引号将user certificate引起来的结果
- [root@zawu globus]# grep user certificate 00.pem
- grep: certificate: 没有那个文件或目录
- #Shell将certificate 解析为文件名,提示没有此文件的错误
- #下面给出00.pem文件中包含user 字符串的行
- 00.pem:The above string is known as your user certificate subject, and it
- 00.pem:uniquely identifies this user.
- 00.pem:To install this user certificate, please save this e-mail message
- 00.pem:/home/globus/.globus/usercert.pem
- #用引号将user certificate引起来后得到正确的结果
- [root@zawu globus]# grep "user certificate" 00.pem
- The above string is known as your user certificate subject, and it
- To install this user certificate, please save this e-mail message
- [root@zawu globus]#
例3-24 首先搜索00.pem文件中包含certificate 字符串的行,由于模式certificate 中不包含空格,因此,是否用引号引起模式对grep 命令不产生影响。当我们要搜索00.pem 文件中包含user certificate 字符串的行时,不用双引号将user certificate 括起来时,Shell 提示没有certificate 这个文件或目录,然后,给出00.pem 文件中包含user 字符串的行,这说明Shell将grep user certificate 00.pem这条命令解释为在certificate和00.pem两个文件中搜索包含user字符串的行,这显然与我们的初衷不符。而当我们用双引号将user certificate括起来后,就得到了正确的结果。
grep支持多文件查询,请看下面的例3-25。
- #例3-25:演示grep 的多文件查询
- [root@zawu globus]# grep Certificate 00.pem 08.pem
- 00.pem:This is a Certificate Request file:
- 00.pem:Certificate Subject:
- 08.pem:Certificate:
- [root@zawu globus]#
上例搜索00.pem 和08.pem 两个文件中包含Certificate 字符串的行,命令逐个给出待搜索的文件,结果打印出所有包含Certificate字符串的行,并以文件名开头。




