免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1255 | 回复: 0
打印 上一主题 下一主题

[linux服务器][终端]用grep在文件内搜索 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-22 23:26 |只看该作者 |倒序浏览

用grep在文件内搜索
一,为何要使用grep ?
   我们在很多情况下都会用到grep,例如:需要找出代码当中包含某个表名的程序文件
   找出包含某个函数的程序,找出包含某个字串的模板文件,
   这时候使用 grep 是最正确的选择
二,grep的基本用法
   1,找出当前目录下包含某个字串的文件
     
    grep的语法如下:
    grep pattern filelist
    说明: pattern 是一个正则表达式
         filelist 是要进行搜索的文件列表

   例子:
   [root@localhost lhd_prod]# grep imgnews *.php
   database.php:$sql_orig = "select * from imgnews where GROUP_ID=2671556";
   mod_cate_news.php:$sql_orig = "select * from imgnews_assoc where SORT_ID=3909";
   mod_cate_news.php:        $sql_imginf = "select * from imgnews where IMGID='".$imgid."'";
   SynPics.php:                                    $sqldel = "delete from imgnews_assoc where IMGID='".$imgid."'";
   说明:我们看到,grep把凡是包含 imgnews这个字串的程序文件列了出来
       但显然 imgnews_assoc 这个数据表名虽然也包含 imgnews 这个字串,但并不是我们想要的结果,
       那么我们如何把包含 imgnews_assoc这个字串的文件过滤掉而不在结果中显示?
       答案是 -v参数
   例子:
   [root@localhost lhd_prod]# grep imgnews *.php | grep -v imgnews_assoc
   database.php:$sql_orig = "select * from imgnews where GROUP_ID=2671556";
   mod_cate_news.php:        $sql_imginf = "select * from imgnews where IMGID='".$outid."'";
   说明: -v参数的含义: invert match
        作用是用来显示不包含匹配字串的行,在本例中: grep -v imgnews_assoc
        就是显示不包含 imgnews_assoc的行
   2,如何得到每个文件中包含我们指定的字串共多少行?
     使用参数 -c
     说明: 参数 -c的含义是count,用来显示文件中包含匹配指定字串的行的数目
     
    例子:
    [root@localhost lhd_prod]# grep -c picnews *.php
    database.php:1
    mod_cate_assoc.php:0
    mod_cate_del117.php:0
    mod_cate_news.php:2
    mod_cate_sync.php:0
    SynPics.php:1
   3,如何只显示包含匹配的字串的文件的文件名?
     只显示文件名,注意:凡是不包含指定字串的,不显示其文件名
     这一点显然和使用 -c参数有所不同
     使用参数:   -l
     
     例子:
     [root@localhost lhd_prod]# grep -l picnews *.php
     database.php
     mod_cate_news.php
     SynPics.php
   4,如何去掉搜索结果中每行前面的文件名?
     就是只显示匹配文件中的匹配指定字串的行的内容,不显示前面的文件名
     使用参数: -h
     例子:
     [root@localhost lhd_prod]# grep -h imgnews *.php
     $sql_orig = "select * from imgnews where GROUP_ID=2671556";
     $sql_orig = "select * from imgnews_assoc where SORT_ID=3909";
              $sql_imginf = "select * from imgnews where IMGID='".$imgid."'";
                                        $sqldel = "delete from imgnews_assoc where IMGID='".$imgid."'";
   5,如何在搜索结果中显示匹配指定字串的行在文件中的行号?
     把匹配指定字串的行在文件中行号显示出来
     用参数: -n
     说明: -n的含义是line number,即行号   
    例子:
    [root@localhost lhd_prod]# grep -n imgnews *.php
    database.php:19:$sql_orig = "select * from imgnews where GROUP_ID=2671556";
    mod_cate_news.php:26:$sql_orig = "select * from imgnews_assoc where SORT_ID=3909";
    mod_cate_news.php:36:        $sql_imginf = "select * from imgnews where IMGID='".$imgid."'";
    SynPics.php:143:                                    $sqldel = "delete from imgnews_assoc where IMGID='".$imgid."'";  
   6,如何在搜索时忽略大小写?
     使用参数 -i
     说明: -i的含义是 ignore case
     例子:
     [root@localhost lhd_prod]# grep -i IMGID *.php
     mod_cate_assoc.php:        $imgid = $row_orig['imgid'];
     mod_cate_assoc.php:        $sql_imginf = "select * from imgout where IMGID='".$imgid."'";
     mod_cate_news.php:        $imgid = $row_orig['IMGID'];
     mod_cate_news.php:        $sql_imginf = "select * from imgnews where IMGID='".$imgid."'";
     SynPics.php:                            $imgid = $grow['IMGID'];
   7,如何在搜索时让指定字串匹配一个词
     使用参数 -w
     说明; -w 的含义是 word,就是要求 pattern必须和整个词匹配
     事实上开始时 -v的例子也可用这个方法来解决
     例子:
     [root@localhost lhd_prod]# grep -w imgnews *.php
     database.php:$sql_orig = "select * from imgnews where GROUP_ID=2671556";
     mod_cate_news.php:        $sql_imginf = "select * from imgnews where IMGID='".$imgid."'";
   
     说明:从上面的例子可以看到, imgnews_assoc虽然也包含 imgnews这个字串,
          但因为不是整字和 imgnews匹配,所以不会在搜索结果中出现
   8,如何在搜索时递归查找整个目录及其子目录?
     使用参数: -R
     说明: -R的含义是: recursive,大家可以注意到linux下有多个命令的递归都使用-R参数,ls即是如此
      例子:
      [root@localhost log]# grep -R root /var/log
      /var/log/audit/audit.log:type=USER_END msg=audit(1228666163.512:59): user pid=5408 uid=500
        auid=500 ses=4 subj=unconfined_u:unconfined_r:unconfined_t:s0
        msg='op=PAM:session_close acct="root" exe="/usr/sbin/userhelper"
        (hostname=?, addr=?, terminal=? res=success)'                                                   
        Binary file /var/log/btmp matches                                                                                                                           
      /var/log/anaconda.syslog:SELinux: initialized (dev rootfs, type rootfs), uses genfs_contexts                                                              
      /var/log/cron:Dec  8 00:01:01 localhost CROND[3025]: (root) CMD (run-parts /etc/cron.hourly)
      
      说明:从例子,我们可以看到,grep递归搜索了 /var/log下的子目录: /var/log/audit/
   9,如何在搜索结果中显示若干行上下文?
     用 -C参数
     形式;   -C n
     说明: n是数字 C的含义是 context
     例子:
     [root@localhost lhd_prod]# grep -C 3 imgnews *.php
     database.php-
     database.php-
     database.php-//得到各个图片
     database.php:$sql_orig = "select * from imgnews where GROUP_ID=2671556";
     database.php-
     database.php-$res_orig = mysql_query($sql_orig,$link_lhd);
     database.php-$num_orig = mysql_num_Rows($res_orig);
     --
     mod_cate_news.php-$dest_piclib = "rare";
     mod_cate_news.php-
     mod_cate_news.php-//
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP