本文共 6375 字,大约阅读时间需要 21 分钟。
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。
grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名。搜索的结果被送到标准输出,不影响原文件内容。
grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。
grep [option] pattern file
用于过滤/搜索的特定字符。可使用正则表达式能多种命令配合使用,使用上十分灵活。
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)实例1:查找指定进程
命令:ps -ef|grep svn
输出:
[root@localhost ~]# ps -ef|grep svnroot 4943 1 0 Dec05 ? 00:00:00 svnserve -d -r /opt/svndata/grape/root 16867 16838 0 19:53 pts/0 00:00:00 grep svn[root@localhost ~]#
说明:
第一条记录是查找出的进程;第二条结果是grep进程本身,并非真正要找的进程。
实例2:查找指定进程个数
命令:ps -ef|grep svn -cps -ef|grep -c svn
输出:
[root@localhost ~]# ps -ef|grep svn -c2[root@localhost ~]# ps -ef|grep -c svn 2[root@localhost ~]#
说明:
实例3:从文件中读取关键词进行搜索
命令:cat test.txt | grep -f test2.txt
输出:
[root@localhost test]# cat test.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatRedhatlinuxmint[root@localhost test]# cat test2.txt linuxRedhat[root@localhost test]# cat test.txt | grep -f test2.txthnlinuxubuntu linuxRedhatlinuxmint[root@localhost test]#
说明:
输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行
实例3:从文件中读取关键词进行搜索 且显示行号
命令:cat test.txt | grep -nf test2.txt
输出:
[root@localhost test]# cat test.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatRedhatlinuxmint[root@localhost test]# cat test2.txt linuxRedhat[root@localhost test]# cat test.txt | grep -nf test2.txt1:hnlinux4:ubuntu linux6:Redhat7:linuxmint[root@localhost test]#
说明:
输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行,并显示每一行的行号
实例5:从文件中查找关键词
命令:grep 'linux' test.txt
输出:
[root@localhost test]# grep 'linux' test.txt hnlinuxubuntu linuxlinuxmint[root@localhost test]# grep -n 'linux' test.txt 1:hnlinux4:ubuntu linux7:linuxmint[root@localhost test]#
说明:
实例6:从多个文件中查找关键词
命令:grep 'linux' test.txt test2.txt
输出:
[root@localhost test]# grep -n 'linux' test.txt test2.txt test.txt:1:hnlinuxtest.txt:4:ubuntu linuxtest.txt:7:linuxminttest2.txt:1:linux[root@localhost test]# grep 'linux' test.txt test2.txt test.txt:hnlinuxtest.txt:ubuntu linuxtest.txt:linuxminttest2.txt:linux[root@localhost test]#
说明:
多文件时,输出查询到的信息内容行时,会把文件的命名在行最前面输出并且加上”:”作为标示符
实例7:grep不显示本身进程
命令:ps aux|grep \[s]shps aux | grep ssh | grep -v "grep"
输出:
[root@localhost test]# ps aux|grep sshroot 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshdroot 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0 root 16901 0.0 0.0 61180 764 pts/0 S+ 20:31 0:00 grep ssh[root@localhost test]# ps aux|grep \[s]sh][root@localhost test]# ps aux|grep \[s]shroot 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshdroot 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0 [root@localhost test]# ps aux | grep ssh | grep -v "grep"root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshdroot 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
说明:
实例8:找出已u开头的行内容
命令:cat test.txt |grep ^u
输出:
[root@localhost test]# cat test.txt |grep ^uubuntuubuntu linux[root@localhost test]#
说明:
实例9:输出非u开头的行内容
命令:cat test.txt |grep ^[^u]
输出:
[root@localhost test]# cat test.txt |grep ^[^u]hnlinuxpeida.cnblogs.comredhatRedhatlinuxmint[root@localhost test]#
说明:
实例10:输出以hat结尾的行内容
命令:cat test.txt |grep hat$
输出:
[root@localhost test]# cat test.txt |grep hat$redhatRedhat[root@localhost test]#
说明:
实例11:
命令: 输出:[root@localhost test]# ifconfig eth0|grep "[0-9]\{ 1,3\}\.[0-9]\{ 1,3\}\.[0-9]\{ 1,3\}\.[0-9]\{ 1,3\}" inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0[root@localhost test]# ifconfig eth0|grep -E "([0-9]{ 1,3}\.){ 3}[0-9]" inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0[root@localhost test]#
说明:
实例12:显示包含ed或者at字符的内容行
命令:cat test.txt |grep -E "ed|at"
输出:
[root@localhost test]# cat test.txt |grep -E "peida|com"peida.cnblogs.com[root@localhost test]# cat test.txt |grep -E "ed|at"redhatRedhat[root@localhost test]#
说明:
实例13:显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行
命令:grep '[a-z]\{ 7\}' *.txt
输出:
[root@localhost test]# grep '[a-z]\{7\}' *.txttest.txt:hnlinuxtest.txt:peida.cnblogs.comtest.txt:linuxmint[root@localhost test]#
说明:
转载地址:http://tfwqd.baihongyu.com/