Linux Shell 脚本编程学习之【第3章 正则表达式 (第二部分) grep命令】

4 grep命令

1、文本搜索工具
2、GREP 是Global search Regular Expression and Print out the line的简称,即全面搜索正则表达式并把行打印出来。
3、grep命令的模式十分灵活,可以是字符串,也可以是变量,还可以是正则表达式。模式中包含空格,则必须用双引号括起来。

4.1 基本用法

选 项 意 义
-c 只输出匹配行的数量
-i 搜索时忽略大小写
-h 查询多文件时不显示文件名
-I 只列出符合匹配的文件名,而不列出具体的匹配行
-n 列出所有的匹配行,并显示行号
-s 不显示不存在或无匹配文本的错误信息
-v 显示不包含匹配文本的所有行
-w 匹配整词
-x 匹配整行
-r 递归搜索,不仅搜索当前工作目录,而且搜索子目录
-q 禁止输出任何结果,以退出状态表示搜索是否成功
-b 打印匹配行距文件头部的偏移量,以字节为单位
-0 与-b选项结合使用,打印匹配的词距文件头部的偏移量,以字节为单位
-E 支持扩展的正则表达式
-F 不支持正则表达式,按照字符串的字面意思进行匹配

4.2 参考命令

命令模式指的是将命令封装成一个整体。

4.2.1 双引号

// 含空格时使用双引号
[rhel@localhost ~]$ grep -i "user for" /etc/passwd
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
geoclue:x:994:991:User for geoclue:/var/lib/geoclue:/sbin/nologin

4.2.2 -c 输出匹配行数

// -c输出匹配的行数

[rhel@localhost ~]$ grep -c -i "user for" /etc/passwd
3

// -n 列出匹配项并加上行号。
[rhel@localhost ~]$ grep -n -i "user for" /etc/passwd
16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
19:colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
27:geoclue:x:994:991:User for geoclue:/var/lib/geoclue:/sbin/nologin

4.2.3 -h 或 -l 不显示或只显示文件名

// -h 显示文件名 -l 只列出符合规范的文件名
[rhel@localhost etc]$ grep -i home passwd*
passwd:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
passwd:db2inst1:x:1001:901::/home/db2inst1:/bin/bash
passwd:db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
passwd:db2as:x:1003:903::/home/db2as:/bin/bash
passwd-:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
passwd-:db2inst1:x:1001:901::/home/db2inst1:/bin/bash
passwd-:db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
[rhel@localhost etc]$ grep -h home passwd*
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
db2inst1:x:1001:901::/home/db2inst1:/bin/bash
db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
db2as:x:1003:903::/home/db2as:/bin/bash
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
db2inst1:x:1001:901::/home/db2inst1:/bin/bash
db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
[rhel@localhost etc]$ grep -l home passwd*
passwd
passwd-

4.2.4 -s 不显示错误信息

[rhel@localhost etc]$ grep user else passwd
grep: else: 没有那个文件或目录
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd:radvd:x:75:75:radvd user:/:/sbin/nologin
passwd:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
passwd:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
passwd:qemu:x:107:107:qemu user:/:/sbin/nologin
passwd:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

[rhel@localhost etc]$ grep -s user else passwd
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd:radvd:x:75:75:radvd user:/:/sbin/nologin
passwd:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
passwd:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
passwd:qemu:x:107:107:qemu user:/:/sbin/nologin
passwd:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

4.2.5 -r 递归显示本级目录及下级目录

[root@localhost etc]# grep  auth pa*
grep: pam.d: 是一个目录
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd-:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
[root@localhost etc]# grep -s auth pa*
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd-:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
[root@localhost etc]# grep -r auth pa*
pam.d/config-util:auth          sufficient      pam_rootok.so
pam.d/config-util:auth          sufficient      pam_timestamp.so
pam.d/config-util:auth          include         system-auth
pam.d/config-util:session               optional        pam_xauth.so

4.2.6 -w 匹配完整词 -x 匹配完整行

[root@localhost etc]# grep tcpd* passwd
tcpdump:x:72:72::/:/sbin/nologin
[root@localhost etc]# grep -w tcpd* passwd
[root@localhost etc]# 
[root@localhost etc]# grep tcpd* passwd
tcpdump:x:72:72::/:/sbin/nologin

[root@localhost etc]# grep -x tcpdump passwd
[root@localhost etc]# grep -x tcpdump:x:72:72::/:/sbin/nologin passwd
tcpdump:x:72:72::/:/sbin/nologin

4.2.7 -q 退出状态

grep命令后一旦加上-q选项, grep将不再输出任何结果,而是以退出状态表示搜索是否成功,退出状态0表示搜索成功,退出状态1表示未搜索到满足模式的文本行,退出状态2表示命令或程序由于错误而未能执行。

[root@localhost etc]# grep -q -x tcpdump:x:72:72::/:/sbin/nologin passwd
[root@localhost etc]# echo $?
0           --退出状态0
[root@localhost etc]# grep -q -x tcpdump passwd
[root@localhost etc]# echo $?
1           --退出状态1
[root@localhost etc]# grep -q -s -x tcpdump passwdmmm
[root@localhost etc]# echo $?
2           --退出状态2

4.2.8 -b 距文件头部的偏移量

grep-b 选项打印匹配行距文件头部的偏移量,以字节为单位。
如果在-b 选项后再加上-o 选项 ,grep命令将打印匹配的词距文件头部的偏移量。

[root@localhost etc]# grep -b bash passwd
0:root:x:0:0:root:/root:/bin/bash
2016:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
2059:db2inst1:x:1001:901::/home/db2inst1:/bin/bash
2105:db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
2151:db2as:x:1003:903::/home/db2as:/bin/bash
[root@localhost etc]# grep -b -o bash passwd
27:bash
2054:bash
2100:bash
2146:bash
2186:bash
[root@localhost etc]# 

4.2.9 -E -F

grep命令的-E 和-F 选项分别等价于grep命令族中的egrep和 fgrep命令

4.3 grep与正则表达式

4.3.1 匹配行首

// 匹配以n开头的行首
[root@localhost etc]# grep ^n passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@localhost etc]# 

4.3.2 查找空白行(^)

// 统计空白行的数量
[root@localhost etc]# grep -c ^$ profile
11

// 统计非空白行行数
[root@localhost etc]# grep -c ^[^$] profile
65

4.3.3 设置大小写(-n)

[root@localhost etc]# grep -i RHEL passwd
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
[root@localhost etc]# grep -n [Rr]hel passwd
40:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash

4.3.4 转义符()

[root@localhost etc]# grep cj.name makedumpfile.conf.sample
##              erase cj.name

[root@localhost rhel]# grep '-{5}' 1.sh
-----dddfsdf
-----sdfsdfasdfaew

4.3.5 POSIX字符

为了保持不同国家的字符编码的一致性, POSIX(Portable Operating System Interface)增 加了特殊的字符类,以[:classname]的格式给出, grep 命令支持POSIX 字符类

类 名 意 义
[:upper:] 表示大写字母[A~Z]
[:lower:] 表示小写字母[a~z]
[:digit:] 表示阿拉伯数字[0~9]
[:alnum:] 表示大小写字母和阿拉伯数字[0~9a~zA~Z]
[:space:] 表示空格或Tab键
[:alpha:] 表示大小写字母[a~zA~Z]
[:cntrl:] 表示Ctrl键
[:graph:]或[:print:] 表示ASCIⅡI码33~126之间的字符
[:xdigit:] 表示16进制数字[0~9 A~Fa~f]

//利用POSIX 字符类搜索以大写字母开头的行
[root@zawu  globus]#  grep  ^[[:upper:]]  00.pem
This is a Certificate Request file:
It   should   be   mailed   to   xd  nidseu edu cn
Certificate Subject:         _            .      .
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 contactMIIB4ZCCAUwCAQAwcTENMAsGA1UEChMER3JpZDETMBEGA1UECxMKR2xvYnVzVGCxMKc2V1LmVkdS5jbjEPMAOGA1UEAxMGZ2XvYnVZMIGfMAOGCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCw50H88r7sAGjQGLZTmMxTiw9AgDqppBMhP6Fg3eJTqBvqBdhaYTRtleSBT/AJUi3rTDRIABJPgu8cZkwb1AE8uEJSeCKwgk3J9QHK2NcZXwIDAQABBAIWADANBgkqhkiG9wOBAQQFAAOBgQBoHRUaaB/Tyu+LuALwnT3Muw/OjDIYxc5aYaA4dwCB6/2yVYyfmyRCNox3rIsyUvqL9p81d/hpNiAB/0OazMBialq5Gcpaansd
[rootezawu   globus]#

//搜索以空格开头文本行
[rootazawu  globus]#  grep  ^[[:space:]]  00.pem
/0=Grid/OU=GlobusTest/OU=simpleCA-seugrid1 .seu .edu .cn/OU=seu .edu .cn/CN=globus
You need not edit this message in any way . Simply
save  this  e-mail  message  to  the  file.

4.3.6 精确匹配

[root@localhost rhel]# grep the 1.sh
Linel:there   are   four   lines   in   this   file 
Line2:this   the   line   2
Line3:this    is    another    line
[root@localhost rhel]# grep "<the>" 1.sh 
Line2:this   the   line   2

[root@localhost rhel]# grep -w the 1.sh
Line2:this   the   line   2

4.3.7 或字符

字符“ | ”是扩展的正则表达式中定义的, grep需要加上-E 选项才能支持它,下面给出grep命令使用“ | ”字符的例子。

[root@localhost rhel]# grep -E "there | another " 1.sh
Linel:there   are   four   lines   in   this   file 
Line3:this    is    another    line

4.4 grep 命令族简介

Linux 系统支持三种形式的grep命令,通常将这三种形式的 grep命令称为 grep命令族, 这三种形式具体为:
● grep: 标准grep 命令,支持基本正则表达式,上面两小节已经对此命令进行了详细 的讨论。
● egrep: 扩展grep 命令,支持基本和扩展正则表达式。
● fgrep: 快速grep 命令,不支持正则表达式,按照字符串的字面意思进行匹配。
egrep 命令与 grep -E 等价, fgrep 命令与 grep -F 等价,在某些Linux 发行版中, egrep 和fgrep都是grep命令的别名,分别将其符号链接到grep-E 和 grep-F 命令。

[root@localhost rhel]# egrep  "there | another " 1.sh
Linel:there   are   four   lines   in   this   file 
Line3:this    is    another    line
[root@localhost rhel]# fgrep another 1.sh
Line3:this    is    another    line