山西网站建设推广,东莞网站优化指导,中国进出口贸易官网,推广运营策略第3章 正则表达式 #xff08;第二部分#xff09; 4 grep命令4.1 基本用法4.2 参考命令4.2.1 双引号4.2.2 -c 输出匹配行数4.2.3 -h 或 -l 不显示或只显示文件名4.2.4 -s 不显示错误信息4.2.5 -r 递归显示本级目录及下级目录4.2.6 -w 匹配完整词 -x 匹配完整行4.2.7 -q 退出… 第3章 正则表达式 第二部分 4 grep命令4.1 基本用法4.2 参考命令4.2.1 双引号4.2.2 -c 输出匹配行数4.2.3 -h 或 -l 不显示或只显示文件名4.2.4 -s 不显示错误信息4.2.5 -r 递归显示本级目录及下级目录4.2.6 -w 匹配完整词 -x 匹配完整行4.2.7 -q 退出状态4.2.8 -b 距文件头部的偏移量4.2.9 -E -F 4.3 grep与正则表达式4.3.1 匹配行首4.3.2 查找空白行^4.3.3 设置大小写-n4.3.4 转义符\4.3.5 POSIX字符4.3.6 精确匹配4.3.7 或字符 4.4 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 双引号
// 含空格时使用双引号
[rhellocalhost ~]$ 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/nologin4.2.2 -c 输出匹配行数
// -c输出匹配的行数[rhellocalhost ~]$ grep -c -i user for /etc/passwd
3// -n 列出匹配项并加上行号。
[rhellocalhost ~]$ 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/nologin4.2.3 -h 或 -l 不显示或只显示文件名
// -h 显示文件名 -l 只列出符合规范的文件名
[rhellocalhost 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
[rhellocalhost 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
[rhellocalhost etc]$ grep -l home passwd*
passwd
passwd-4.2.4 -s 不显示错误信息
[rhellocalhost 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[rhellocalhost 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/nologin4.2.5 -r 递归显示本级目录及下级目录
[rootlocalhost 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
[rootlocalhost 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
[rootlocalhost 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.so4.2.6 -w 匹配完整词 -x 匹配完整行
[rootlocalhost etc]# grep tcpd* passwd
tcpdump:x:72:72::/:/sbin/nologin
[rootlocalhost etc]# grep -w tcpd* passwd
[rootlocalhost etc]#
[rootlocalhost etc]# grep tcpd* passwd
tcpdump:x:72:72::/:/sbin/nologin[rootlocalhost etc]# grep -x tcpdump passwd
[rootlocalhost etc]# grep -x tcpdump:x:72:72::/:/sbin/nologin passwd
tcpdump:x:72:72::/:/sbin/nologin4.2.7 -q 退出状态
grep命令后一旦加上-q选项 grep将不再输出任何结果而是以退出状态表示搜索是否成功退出状态0表示搜索成功退出状态1表示未搜索到满足模式的文本行退出状态2表示命令或程序由于错误而未能执行。
[rootlocalhost etc]# grep -q -x tcpdump:x:72:72::/:/sbin/nologin passwd
[rootlocalhost etc]# echo $?
0 --退出状态0
[rootlocalhost etc]# grep -q -x tcpdump passwd
[rootlocalhost etc]# echo $?
1 --退出状态1
[rootlocalhost etc]# grep -q -s -x tcpdump passwdmmm
[rootlocalhost etc]# echo $?
2 --退出状态24.2.8 -b 距文件头部的偏移量
grep-b 选项打印匹配行距文件头部的偏移量以字节为单位。 如果在-b 选项后再加上-o 选项 grep命令将打印匹配的词距文件头部的偏移量。
[rootlocalhost 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
[rootlocalhost etc]# grep -b -o bash passwd
27:bash
2054:bash
2100:bash
2146:bash
2186:bash
[rootlocalhost etc]# 4.2.9 -E -F
grep命令的-E 和-F 选项分别等价于grep命令族中的egrep和 fgrep命令
4.3 grep与正则表达式
4.3.1 匹配行首
// 匹配以n开头的行首
[rootlocalhost 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
[rootlocalhost etc]# 4.3.2 查找空白行^
// 统计空白行的数量
[rootlocalhost etc]# grep -c ^$ profile
11// 统计非空白行行数
[rootlocalhost etc]# grep -c ^[^$] profile
65
4.3.3 设置大小写-n
[rootlocalhost etc]# grep -i RHEL passwd
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
[rootlocalhost etc]# grep -n [Rr]hel passwd
40:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash4.3.4 转义符\
[rootlocalhost etc]# grep cj\.name makedumpfile.conf.sample
## erase cj.name[rootlocalhost rhel]# grep \-\{5\} 1.sh
-----dddfsdf
-----sdfsdfasdfaew4.3.5 POSIX字符
为了保持不同国家的字符编码的一致性 POSIX(Portable Operating System Interface)增 加了特殊的字符类以[:classname]的格式给出 grep 命令支持POSIX 字符类
类 名意 义[:upper:]表示大写字母[AZ][:lower:]表示小写字母[az][:digit:]表示阿拉伯数字[09][:alnum:]表示大小写字母和阿拉伯数字[09azAZ][:space:]表示空格或Tab键[:alpha:]表示大小写字母[a~zAZ][:cntrl:]表示Ctrl键[:graph:]或[:print:]表示ASCIⅡI码33126之间的字符[:xdigit:]表示16进制数字[0~9 AFaf] //利用POSIX 字符类搜索以大写字母开头的行
[rootzawu 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/TyuLuALwnT3Muw/OjDIYxc5aYaA4dwCB6/2yVYyfmyRCNox3rIsyUvqL9p81d/hpNiAB/0OazMBialq5Gcpaansd
[rootezawu globus]#//搜索以空格开头文本行
[rootazawu globus]# grep ^[[:space:]] 00.pem
/0Grid/OUGlobusTest/OUsimpleCA-seugrid1 .seu .edu .cn/OUseu .edu .cn/CNglobus
You need not edit this message in any way . Simply
save this e-mail message to the file.4.3.6 精确匹配
[rootlocalhost rhel]# grep the 1.sh
Linel:there are four lines in this file
Line2:this the line 2
Line3:this is another line
[rootlocalhost rhel]# grep \the\ 1.sh
Line2:this the line 2[rootlocalhost rhel]# grep -w the 1.sh
Line2:this the line 24.3.7 或字符
字符“ | ”是扩展的正则表达式中定义的 grep需要加上-E 选项才能支持它下面给出grep命令使用“ | ”字符的例子。
[rootlocalhost rhel]# grep -E there | another 1.sh
Linel:there are four lines in this file
Line3:this is another line4.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 命令。
[rootlocalhost rhel]# egrep there | another 1.sh
Linel:there are four lines in this file
Line3:this is another line
[rootlocalhost rhel]# fgrep another 1.sh
Line3:this is another line