Shell文本处理三剑客之一grep

Shell文本处理三剑客之一grep

grep命令:

grep(Global search regular expression andprint out the line(全面搜索研究正则表达式并显示出来),grep命令时支持正则表达式(元字符有特殊含义)

作用:grep命令是一种强大的文本搜索工具,根据用户指定的模式对目标文件进行匹配(pattern):由正则表达式或者字符及基本文本字符所编写的过滤条件

 

grep (模式)匹配字符 文件  grep命令默认情况下打印出包含模式所有的行

 

grep  -c          只输出匹配行的数量

grep  -i                    不区分大小写

grep  -h          查询多文件时不显示文件名

grep  -I                    只列出符合匹配的文件名,而不列出具体匹配的行

grep  -n          列出所有的匹配行,并显示行号

grep  -s          不显示不存在或无匹配文本的错误信息

grep  -v          显示不包含匹配文本的所有行(反向匹配)

grep  -w                  匹配整词,不识别关键字中的元字符,之间按字面意思匹配

grep  -x          匹配整行

grep  -r      第归搜索,打印当前工作目录及子目录中所有文件中包含字符串关键字的行

grep  -q          禁止输出任何结果,以退出状态表示搜索是否成功

grep  -b          打印匹配行距文件头部的偏移量,以字节为单位

grep  -o          与-b结合使用,打印匹配的词距文件头部的偏移量,以字节为单位

grep  -E          支持扩展的正则表达式

grep  -F          不支持正则表达式,按照字符串的字面意思进行匹配

 

 

grep (模式)匹配字符 文件

[root@localhost grep]# grep root /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

grep  -c          只输出匹配行的数量

[root@localhost grep]# grep root -c /etc/passwd

2

grep  -n         列出所有的匹配行,并显示行号

[root@localhost grep]# grep -n root /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

10:operator:x:11:0:operator:/root:/sbin/nologin

grep  -v          显示不包含关键字的所有行(反向匹配)

[root@localhost grep]# grep -v root /etc/passwd

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

……

 

grep  -vc      输出不包含关键字的行数

[root@localhost grep]# grep -vc root /etc/passwd

42

 

grep  -i                    不区分大小写匹配

[root@localhost grep]# grep -i root passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

 

grep ro*t passwd      *在正则表达式中表示  匹配0个或多个在*前的普通字符

 

[root@localhost grep]# grep ro*t passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin

[root@localhost grep]# vim /etc/passwd

[root@localhost grep]# grep ro*t passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin

 

grep  -s          不显示不存在或无匹配文本的错误信息

[root@localhost grep]# grep -s root dd

[root@localhost grep]# grep root dd

grep: dd: No such file or directory      #正常情况下会报错

grep  -r  第归搜索,打印当前工作目录及子目录中所有文件中包含字符串关键字的行

 

[root@localhost grep]# grep -r passwd  /etc/

/etc/services:kpasswd         464/tcp         kpwd            # Kerberos "passw"

/etc/services:kpasswd         464/udp         kpwd            # Kerberos "passw"

/etc/services:passwd_server   752/udp         qrh             # Kerberos passwd server

/etc/services:rpasswd         774/tcp                 #

/etc/selinux/semanage.conf:# usepasswd check tells semanage to scan all pass word records for home directories

/etc/selinux/semanage.conf:usepasswd=False

……

 

 

grep  -w                 匹配整词,不识别关键字中的元字符,之间按字面意思匹配

 

[root@localhost grep]# vim passwd

[root@localhost grep]# grep roo* passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin

tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin

setroubleshoot:x:992:989::/var/lib/setroubleshoot:/sbin/nologin

rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin

chrony:x:991:988::/var/lib/chrony:/sbin/nologin

roooooooooooooooot

[root@localhost grep]# grep -w roo* passwd

[root@localhost grep]# vim passwd

[root@localhost grep]# grep -w roo* passwd

roo*

[root@localhost grep]#

 

grep  -x 匹配整行,只有当文件中有整行的内容与模式匹配时,才会输出该行的结果

[root@localhost grep]# grep -w Future future.txt

Hello Future

Future

Future Life

[root@localhost grep]# grep -x Future future.txt

Future

grep  -q         禁止输出任何结果,以退出状态表示搜索是否成功

[root@localhost grep]# grep -q -x Future future.txt   #不输出任何结果

[root@localhost grep]# echo $?     

0             #搜索成功,打印状态码为0

[root@localhost grep]# grep -q -x "Future haha" future.txt

[root@localhost grep]# echo $?

1             #搜索失败,状态码为非 0

grep -c ^$       查找空白行且只打印行数

[root@localhost grep]# grep -c ^$ passwd

4

 

grep -c ^[^$]     只打印出文件中不是空白行的行数  ^[]  表示匹配不满足[]

[root@localhost grep]# grep -c ^[^$] passwd

48

 

 

grep -n [Rr]oot passwd  匹配含有Root或者root的行,且显示行号

 

[root@localhost grep]# grep -n [Rr]oot passwd

1:root:x:0:0:root:/root:/bin/bash

10:operator:x:11:0:operator:/root:/sbin/nologin

49:Rootdfsdfsfs

grep ^/..../  匹配以/..../开头的行

[root@localhost grep]# grep ^/..../ passwd

/..../fdsfsdfsdfs

 

grep '[[:alpha:]]\{3\}t' 表示匹配前三个为任意字母最后一个字母是t的行

[:alpha:]  :通配符表示字母 \{3\} :表示重复匹配3次

 

[root@localhost grep]# grep '[[:alpha:]]\{3\}t'  /etc/passwd

root:x:0:0:root:/root:/bin/bash

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

operator:x:11:0:operator:/root:/sbin/nologin

systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin

发布了150 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43309149/article/details/104361068