Linux 系统中 grep 是一种强大的文本搜索工具,它可以使用正则表达式搜索文本,并把匹配的结果打印出来。grep 全称是 Global Regular Expression Print ,它表示全局正则表达式,它的使用权限是所有用户;
grep 的工作方式是这样,它在一个或多个文件中搜索字符串模板。如果模板包含空格,则必须被引用,模板后的所有字符串被看作文件名。搜索的结果被送到标准输出,不影响原文件内容;
grep 可用于 shell 脚本,因为 grep 通过一个返回值来说明搜索的状态,如果搜索成功,则返回0,搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可以进行一些自动化的文件处理工作;
命令格式 - grep [option] pattern file
命令功能 - 用于过滤/搜索特定字符。可使用正则表达式的多种命令配合使用
命令参数 -
规则表达式
grep的规则表达式
^ #锚定行的开始 如:’^grep'匹配所有以grep开头的行。
$ #锚定行的结束 如:’grep$’匹配所有以grep结尾的行。
. #匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。
* #匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。
.* #一起用代表任意字符。
[] #匹配一个指定范围内的字符,如’[Gg]rep'匹配Grep和grep。
[^] #匹配一个不在指定范围内的字符,如:’[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。
\(..\) #标记匹配字符,如’\(love\)',love被标记为1。
\< #锚定单词的开始,如:’\<grep'匹配包含以grep开头的单词的行。
\> #锚定单词的结束,如’grep\>'匹配包含以grep结尾的单词的行。
x\{m\} #重复字符x,m次,如:’0\{5\}'匹配包含5个o的行。
x\{m,\} #重复字符x,至少m次,如:’o\{5,\}'匹配至少有5个o的行。
x\{m,n\} #重复字符x,至少m次,不多于n次,如:’o\{5,10\}'匹配5--10个o的行。
\w #匹配文字和数字字符,也就是[A-Za-z0-9],如:’G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。
\W #\w的反置形式,匹配一个或多个非单词字符,如点号句号等。
\b #单词锁定符,如: ‘\bgrep\b'只匹配grep。
POSIX字符
为了在不同国家的字符编码中保持一至,POSIX(The Portable Operating System Interface) 增加了特殊的字符类,如 **[:alnum:]** 是 [A-Za-z0-9] 的另一个写法。要把它们放到 [] 号内才能成为正则表达式,如 [A- Za-z0-9] 或 [[:alnum:]] 。在 linux 下的 grep 除 fgrep 外,都支持 POSIX 的字符类。
[:alnum:] #文字数字字符
[:alpha:] #文字字符
[:digit:] #数字字符
[:graph:] #非空字符(非空格、控制字符)
[:lower:] #小写字符
[:cntrl:] #控制字符
[:print:] #非空字符(包括空格)
[:punct:] #标点符号
[:space:] #所有空白字符(新行,空格,制表符)
[:upper:] #大写字符
[:xdigit:] #十六进制数字(0-9,a-f,A-F)
命令实例
实例一:查找指定进程
命令
xxxxxxxxxx11ps -ef|grep svn输出
xxxxxxxxxx41[root@localhost ~]# ps -ef|grep svn2root 4943   1      0  Dec05 ?   00:00:00 svnserve -d -r /opt/svndata/grape/3root 16867 16838  0 19:53 pts/0    00:00:00 grep svn4[root@localhost ~]#说明
第一条记录是查找出的进程;第二天记录是grep进程本身,并非真正要找的进程;
实例二:查找指定进程个数
命令
xxxxxxxxxx21ps -ef|grep svn -c2ps -ef|grep -c svn输出
xxxxxxxxxx51[root@localhost ~]# ps -ef|grep svn -c223[root@localhost ~]# ps -ef|grep -c svn 425[root@localhost ~]#说明
实例三:从文件中读取关键词进行搜索
命令
xxxxxxxxxx11cat test.txt | grep -f test2.txt输出
xxxxxxxxxx171[root@localhost test]# cat test.txt 2hnlinux3peida.cnblogs.com4ubuntu5ubuntu linux6redhat7Redhat8linuxmint9[root@localhost test]# cat test2.txt 10linux11Redhat12[root@localhost test]# cat test.txt | grep -f test2.txt13hnlinux14ubuntu linux15Redhat16linuxmint17[root@localhost test]#说明
输出 test.txt文件中含有 test2.txt 文件中读取的关键词的内容行;
实例四:从文件中读取关键词进行搜索 且显示行号
命令
xxxxxxxxxx11cat test.txt | grep -nf test2.txt输出
xxxxxxxxxx171[root@localhost test]# cat test.txt 2hnlinux3peida.cnblogs.com4ubuntu5ubuntu linux6redhat7Redhat8linuxmint9[root@localhost test]# cat test2.txt 10linux11Redhat12[root@localhost test]# cat test.txt | grep -nf test2.txt131:hnlinux144:ubuntu linux156:Redhat167:linuxmint17[root@localhost test]#说明
输出 test.txt文件中含有 test2.txt 文件中读取的关键词的内容行,并显示每一行的行号;
实例五:从文件中查找关键词
命令
xxxxxxxxxx11grep 'linux' test.txt输出
xxxxxxxxxx91[root@localhost test]# grep 'linux' test.txt 2hnlinux3ubuntu linux4linuxmint5[root@localhost test]# grep -n 'linux' test.txt 61:hnlinux74:ubuntu linux87:linuxmint9[root@localhost test]#说明
实例六:从多个文件中查找关键词
命令
xxxxxxxxxx11grep 'linux' test.txt test2.txt输出
xxxxxxxxxx111[root@localhost test]# grep -n 'linux' test.txt test2.txt 2test.txt:1:hnlinux3test.txt:4:ubuntu linux4test.txt:7:linuxmint5test2.txt:1:linux6[root@localhost test]# grep 'linux' test.txt test2.txt 7test.txt:hnlinux8test.txt:ubuntu linux9test.txt:linuxmint10test2.txt:linux11[root@localhost test]#说明
多文件时,输出查询到的信息内容行时,会把文件的命名在行最前面输出并且加上":"作为标示符;
实例七:grep不显示本身进程
命令
xxxxxxxxxx21ps aux|grep \[s]sh2ps aux | grep ssh | grep -v "grep"输出
xxxxxxxxxx111[root@localhost test]# ps aux|grep ssh2root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd3root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 4root  16901  0.0  0.0  61180   764 pts/0  S+   20:31   0:00 grep ssh5[root@localhost test]# ps aux|grep \[s]sh]6[root@localhost test]# ps aux|grep \[s]sh7root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd8root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 9[root@localhost test]# ps aux | grep ssh | grep -v "grep"10root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd11root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0说明
实例八:找出以u开头的行内容
命令
xxxxxxxxxx11cat test.txt |grep ^u输出
xxxxxxxxxx41[root@localhost test]# cat test.txt |grep ^u2ubuntu3ubuntu linux4[root@localhost test]#说明
实例九:输出非u开头的行内容
命令
xxxxxxxxxx11cat test.txt |grep ^[^u]输出
xxxxxxxxxx71[root@localhost test]# cat test.txt |grep ^[^u]2hnlinux3peida.cnblogs.com4redhat5Redhat6linuxmint7[root@localhost test]#说明
实例十:输出以hat结尾的行内容
命令
xxxxxxxxxx11cat test.txt |grep hat$输出
xxxxxxxxxx41[root@localhost test]# cat test.txt |grep hat$2redhat3Redhat4[root@localhost test]#说明
实例十一:显示包含ed或者at字符的内容行
命令
xxxxxxxxxx11cat test.txt |grep -E "ed|at"输出
xxxxxxxxxx61[root@localhost test]# cat test.txt |grep -E "peida|com"2peida.cnblogs.com3[root@localhost test]# cat test.txt |grep -E "ed|at"4redhat5Redhat6[root@localhost test]#说明
实例十二:显示当前目录下以.txt 结尾的文件中所包含的每个字符串至少有7个连续小写字符的字符串的行命令
命令
xxxxxxxxxx11grep '[a-z]\{7\}' *.txt输出
xxxxxxxxxx51[root@localhost test]# grep '[a-z]\{7\}' *.txt2test.txt:hnlinux3test.txt:peida.cnblogs.com4test.txt:linuxmint5[root@localhost test]#说明