免费注册 查看新帖 |

Chinaunix

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

grep学习笔记 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-12 17:01 |只看该作者 |倒序浏览

                                                grep对文本进行查找,匹配相应模式,找到匹配模式后,打印相应行
grep格式:
grep [选项] 正则表达式(即相应模式) [文件]    注意:正则表达式中的字符串最好用“”括起来 调用变量也要用“”括起来 正则表达式本身最好用''括起来 防止出错
选项:
-c 只输出匹配行的计数(即匹配的有多少行)
   eg:  ]# grep  dec data.f data.g
        data.f:219      dec     2CC1999         CAD     23.00   PLV2C   68
        data.g:48       dec     3bc1997         lpsx    68.00   LVX2A   138
        data.g:219      dec     2cc1999         CAD     23.00   PLV2C   68
        ]# grep  -c dec data.f data.g
        data.f:1
        data.g:2
-i 表达式中不分大小写Ignore case distinctions in both the PATTERN and the input files.
-h 查询多个文件的时候 不显示结果是哪个文件中的
   eg:  ]# grep -h dec data.f data.g
        219     dec     2CC1999         CAD     23.00   PLV2C   68
        48      dec     3bc1997         lpsx    68.00   LVX2A   138
        219     dec     2cc1999         CAD     23.00   PLV2C   68
        ]# grep  dec data.f data.g
        data.f:219      dec     2CC1999         CAD     23.00   PLV2C   68
        data.g:48       dec     3bc1997         lpsx    68.00   LVX2A   138
        data.g:219      dec     2cc1999         CAD     23.00   PLV2C   68
-l 查询多个文件时只显示包含匹配字符的文件的文件名(不显示结果)
  eg:  ]# grep   -l Dec data.f data.g
       data.f
        ]# grep   Dec data.f data.g
       data.f:48       Dec     3BC1997         LPSX    68.00   LVX2A   138
-n 显示匹配的行和行号
-s 不显示不存在或者无匹配文本的error信息
-v 显示不包含匹配文本的所有行
-E 使用扩展的正则表达式模式
   eg:]# grep '219|216' data.f data.g
      ]# grep -E '219|216' data.f data.g
      data.f:219      dec     2CC1999         CAD     23.00   PLV2C   68
      data.f:216      sept    3ZL1998         USP     86.00   KVM9E   234
      data.g:219      dec     2cc1999         CAD     23.00   PLV2C   68
      data.g:216      sept    3zl1998         USP     86.00   KVM9E   234
grep不只是用于文件 亦可用于字符串
eg: ]# str="last sunday i went to a therate"
    ]# echo $str|grep "went"
    last sunday i went to a therate
egrep
-f 可以以一个文件作为保存的字符串 然后传给egrep作为参数 ps是建立的一个文本文件,文件名为ps,内    容就是 dec
eg: ]# egrep ps data.f data.g
    data.g:48       dec     3bc1997         lpsx    68.00   LVX2A   138
    data.g:47       oct     3zl1998         lpsx    43.00   KVM9D   512
    ]# egrep -f ps data.f data.g
    data.f:219      dec     2CC1999         CAD     23.00   PLV2C   68
    data.g:48       dec     3bc1997         lpsx    68.00   LVX2A   138
    data.g:219      dec     2cc1999         CAD     23.00   PLV2C   68
               
               
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/31056/showart_1409334.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP