grep查找路径下所有文件内容

一、需求描述

      /etc目录包含了多个目录及脚本文件,现在需要确定/etc路径下的文件中,哪些文件包含了“logread”字符串。

      在使用linux时,我们一般用find命令来进行文件查找。但find命令是根据文件的属性进行查找,如文件名,文件大小,所有者,所属组,是否为空,访问时间,修改时间等。无法查找文件内容 ,满足不了上述需求。

      此时,我们可以考虑用grep命令来实现该需求。grep是根据文件的内容进行查找,会对文件的每一行按照给定的模式(patter)进行匹配查找。       

二、需求实现

      输入命令:grep "logread" /etc -nr 即可完成查找

[root@localhost ~]grep "logread" /etc -nr
grep: /etc/fstab: No such file or directory
/etc/init.d/fhlog:8:PROG=/sbin/logread
/etc/init.d/fhlog:10:PID_FILE="/var/run/logread.fhlog.pid"
grep: /etc/ppp/resolv.conf: No such file or directory
/etc/rc.d/K89fhlog:8:PROG=/sbin/logread
/etc/rc.d/K89fhlog:10:PID_FILE="/var/run/logread.fhlog.pid"
/etc/rc.d/S21fhlog:8:PROG=/sbin/logread
/etc/rc.d/S21fhlog:10:PID_FILE="/var/run/logread.fhlog.pid"
、grep命令介绍

1.作用
      Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expr
ession Print,表示全局正则表达式版本,它的使用权限是所有用户。

2.格式
grep [options]

3.主要参数
[options]主要参数:
-c:只输出匹配行的计数。
-I:不区分大 小写(只适用于单字符)。
-h:查询多文件时不显示文件名。
-l:查询多文件时只输出包含匹配字符的文件名。
-n:显示匹配行及 行号。
-s:不显示不存在或无匹配文本的错误信息。
-v:显示不包含匹配文本的所有行。

pattern正则表达式主要参数:
\: 忽略正则表达式中特殊字符的原有含义。
^:匹配正则表达式的开始行。
$: 匹配正则表达式的结束行。
\<:从匹配正则表达 式的行开始。
\>:到匹配正则表达式的行结束。
[ ]:单个字符,如[A]即A符合要求 。
[ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
。:所有的单个字符。
* :有字符,长度可以为0。

4.grep命令使用简单实例
$ grep ‘test’ d*
显示所有以d开头的文件中包含 test的行。
$ grep ‘test’ aa bb cc
显示在aa,bb,cc文件中匹配test的行。
$ grep ‘[a-z]\{5\}’ aa
显示所有包含每个字符串至少有5个连续小写字符的字符串的行。
$ grep ‘w\(es\)t.*\1′ aa
如果west被匹配,则es就被存储到内存中,并标记为1,然后搜索任意个字符(.*),这些字符后面紧跟着 另外一个es(\1),找到就显示该行。如果用egrep或grep -E,就不用”\”号进行转义,直接写成’w(es)t.*\1′就可以了。

四、经典应用场景

      除非要精确区分大小写,否则请加上-i来忽略大小写

1)结合find命令和管道
       你的一个音乐文件夹里有多种格式的文件,而你只想找到艺术家jay的mp3文件,并且不含有任何的混合音轨
[root@localhost ~]#find . -name ".mp3" | grep -i jay | grep -vi "remix"
       分析: 1)使用find -name 来列出所有mp3文件,重定向给grep
2) 使用grep -i 来查找包含jay的行
3)使用grep -vi 来查找不包含remix的行

2)-A -B -C

      很多时候,我们并关心匹配行而是关心匹配行的上下文。这时候-A -B -C就有用了
-A n 后n行,A记忆为(After)
-B n 前n行,B记忆为(Before)
-C n 前n行,后n行,C记忆为(Center)
举例

[root@localhost ~]# ifconfig | grep -A 2 "Link encap"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F3:38:15  
          inet addr:192.168.91.129  Bcast:192.168.91.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fef3:3815/64 Scope:Link
--
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host


[root@localhost ~]#  ifconfig | grep -C 2 "lo"
          Interrupt:67 Base address:0x2024 


lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
3) 用-c来统计数目

  你手头有一个很大的文件,这个文件包含网址,比如www.baidu.com tieba.baidu.com等等。你想要知道有多少个隶属于百度的网址。

root@localhost ~]# grep -c "*baidu.com*" filename
例子
[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -cn ".*baidu.com.*" file.txt 
3

4) -r 递归查找子目录

      查找当前目录极其子目录下面包含匹配字符的文件
      查找子目录,匹配后输出行号,这里的点表示当前目录

[root@localhost ~]# grep -nr HELLO_HWC_CSND_BLOG* .

例子:

[root@localhost ~]# grep -nr baidu .
./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 -lr HELLO_HWC_CSND_BLOG* .

例子:

[root@localhost ~]# grep -lr baidu .
./file.txt
./test/test.txt

5)查找不包含某一个目录

[root@localhost ~]#grep -R --exclude-dir=node_modules 'some pattern' /path/to/search

例子

[root@localhost ~]# ls
anaconda-ks.cfg  Desktop  file.txt  find.result  install.log  install.log.syslog  test
[root@localhost ~]# grep -r baidu .
./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: unrecognized option `--exclude-dir=test'

说明版本过老,更新下就ok

6)查找IP地址
这里用到了-o和-P命令
我们通过man grep查看
-o, --only-matching:
              Show only the part of a matching line that matches PATTERN.
-P, --perl-regexp:
              Interpret PATTERN as a Perl regular expression.
也就是说-o,只显示匹配行中匹配正则表达式的那部分

-P,作为Perl正则匹配

[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}" file.txt
192.168.0.1
162.12.0.123

7)查找邮箱

[root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt

例子

[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt
njuhwc@163.com
njuhwc@gmil.com



参考文章:http://www.open-open.com/lib/view/open1426417914694.html

  • 12
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值