grep命令介绍功能Linux系统中的grep命令是一个强大的文本搜索工具,可以使用正则表达式搜索文本并打印出匹配的行。grep的全称是GlobalRegularExpressionPrint,意思是全局正则表达式版本,它的使用权限是对所有用户开放的。格式grep[options]mainparameters[options]mainparameters:-c:只输出匹配行数。-I:不区分大小写(只适用于单个字符)。-h:查询多个文件时不显示文件名。-l:查询多个文件时,只输出包含匹配字符的文件名。-n:显示匹配的行和行号。-s:不显示不存在或没有匹配文本的错误信息。-v:显示所有不包含匹配文本的行。模式正则表达式的主要参数:\:忽略正则表达式中特殊字符的本义。^:匹配正则表达式的起始行。$:匹配正则表达式行的结尾。\<:从匹配正则表达式的行开始。\>:到匹配正则表达式的行尾。[]:单个字符,如[A]表示A满足要求。[-]:范围,如[A-Z],即A、B、C到Z都满足要求。.:所有单个字符。*:有字符,长度可以为0。grep命令用一个简单的例子$grep'test'd*来显示以d开头的文件中所有包含test的行。$grep'test'aabbcc显示aa、bb、cc文件中匹配test的行。$grep'[a-z]\{5\}'aa显示包含字符串的所有行,每个字符串至少有5个连续的小写字符。$grep'wesest.*\1'aa如果匹配到west,则es存入内存并标记为1,然后搜索任意数量的字符(.*),后面跟着另一个es(\1),该行找到后显示。如果使用egrep或grep-E,则不需要使用“\”符号进行转义,直接写'w(es)t.*\1'即可。经典应用场景除非你想区分大小写,否则请加-i忽略大小写结合find命令和管道实现你的一个音乐文件夹下有多种格式的文件,你只想找到艺术家jay的mp3文件,并且不包含任何混合音轨命令[root@localhost~]#find。-名称“.mp3”|grep-i杰伊|grep-vi"remix"分析1)使用find-name列出所有mp3文件,重定向到grep2)使用grep-i查找包含jay的行3)使用grep-vi查找不包含remix-A-B-C的行很多时候,我们不关心匹配行,而是关心匹配行上下文。这时候-A-B-C就有用了-Ann行之后,A在n行之前存为(After)-Bn,B在n行之前存为(Before)-Cn,n行之后,C存储Examplefor(Center)[root@localhost~]#ifconfig|grep-A2“Linkencap”eth0Linkencap:EthernetHWaddr00:0C:29:F3:38:15inetaddr:192.168.91.129Bcast:192.168.91.255Mask:255.255.255.0inet6addr:fe80::20c:29ff:fef3:3815/64作用域:Link--loLinkencap:LocalLoopbackinetaddr:127.0.0.1Mask:255.0.0.0inet6addr:::1/128Scope:Host[root@localhost~]#ifconfig|grep-C2"lo"Interrupt:67Baseaddress:0x2024loLinkencap:LocalLoopbackinetaddr:127.0.0.1Mask:255.0.0.0inet6addr:::1/128Scope:Host使用-c来统计数量。您手头有一个大文件。该文件包含网址,如www.baidu.comtieba.baidu.com等。你想知道有多少网址属于百度。命令root@localhost~]#grep-c"*baidu.com*"filenameexample[root@localhost~]#catfile.txtwtmpbeginsMonFeb2414:26:082014192.168.0.1162.12.0.123"123"123""123njuhwc@163.comnjuhwc@gmil.com123www.baidu.comtieba.baidu.comwww.google.comwww.baidu.com/search/index[root@localhost~]#grep-cn".*baidu.com.*"file.txt3-r递归搜索子目录在当前目录及其子目录下搜索包含匹配字符的文件搜索子目录,匹配后输出行号,其中点代表当前目录命令(查找当前路径下的所有文件,包含baidu的文件)[root@localhost~]#grep-nrHELLO_HWC_CSND_BLOG*.Example#查找当前路径下所有包含`baidu`的文件[root@localhost~]#grep-nrbaidu../file.txt:8:www.baidu.com./file.txt:9:tieba.baidu.com./file.txt:11:www.baidu.com/search/index./test/test.txt:1:http://www.baidu.com命令(搜索子目录,匹配后只输出文件名)[root@localhost~]#grep-lrHELLO_HWC_CSND_BLOG*.Example#搜索子目录,匹配后只输出文件名[root@localhost~]#grep-lrbaidu../file.txt./test/test.txt搜索不包含某目录的命令[root@localhost~]#grep-R--exclude-dir=node_modules'somepattern'/path/to/searchexample[root@localhost~]#lsanaconda-ks.cfg桌面文件.txtfind.resultinstall.loginstall.log.syslogtest[root@localhost~]#grep-rbaidu../file.txt:www.baidu.com./file.txt:tieba.baidu.com./file.txt:www.baidu.com/search/index./test/test.txt:http://www.baidu.com这个时候如果我们不想包含test目录[root@localhost~]#grep-R--exclude-dir=text"baidu"../file.txt:www.baidu.com./file.txt:tieba.baidu.com./file.txt:www.baidu.com/search/index如果出错报grep:unrecognizedoption`--exclude-dir=test'说明版本太旧,更新后就ok了。查找IP地址的命令显示,这里使用了-o和-P命令。我们可以通过mangrep检查-o,--only-matching:只显示匹配行中匹配PATTERN的部分。-P,--perl-regexp:将PATTERN解释为Perl正则表达式。即-o,只显示匹配行中正则表达式的匹配部分-P,以Perl正则匹配为例[root@localhost~]#catfile.txtwtmpbeginsMonFeb2414:26:082014192.168.0.1162.12.0.123"123"123""123njuhwc@163.comnjuhwc@gmil.com123www.baidu.comtieba.baidu.comwww.google.comwww.baidu.com/search/index[root@localhost~]#grep-oP"([0-9]{1,3}\.){3}[0-9]{1,3}"文件.txt192.168.0.1162.12.0.123搜索邮箱命令说明[root@localhost~]#grep-oP"[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+"file.txt示例[root@localhost~]#catfile.txtwtmpbeginsMonFeb2414:26:082014192.168.0.1162.12.0.123"123"123""123njuhwc@163.comnjuhwc@gmil.com123www.baidu.comtieba.baidu.comwww.google.comwww.baidu.com/search/index[root@localhost~]#grep-oP"[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+"file.txtnjuhwc@163.com
