grep的实例.doc_第1页
grep的实例.doc_第2页
grep的实例.doc_第3页
grep的实例.doc_第4页
grep的实例.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、grep 参数使用Gun grep 选项-b 在搜索到的行的前面打印该行所在的块号码。-c 只显示有多少行匹配 ,而不具体显示匹配的行-h 不显示文件名-i 在字符串比较的时候忽略大小写-l 只显示包含匹配模板的行的文件名清单,不同项目之间用换行符分隔-L 打印不匹配模板的文件名清单-n 在每一行前面打印该行在文件中的行数-s 静默工作,除非出现错误信息否则不打印任何信息,这个功能在检测退出状态的时候有用-v 反检索,只显示不匹配的行-w -Ax 在匹配指定行打印完毕后,再打印x行(向原文件匹配行下x行)-By 在匹配指定行前面打印y行(在原文件匹配行上面打印y行)-Cz 在匹配行前后打印z行(在原文件匹配行上下打印z行)-b 在每一行前面打印字符偏移量-f file 从文件file中提取模板。空文件中包含0个模板-q取消标准输出,跟-n功能是一样的-s 不显示关于不存在或者无法读文件的错误信息-w只打印以单词形式匹配模板的行,模板可以是包含数字、字符和下划线的字符串-x 只打印整行匹配的行-y 用法同-i-U把文件作为二进制文件,这个选项只在MSDOS和MSWindows中被支持(这个参数没有明白,请过路高人指点,非常感谢)-u 按照unix风格报告字符偏移量。只在-b选项同时被使用的时候才有效。这个选项只在MSDOS和MSWindows中被支持grep $name file 把变量$name 的值作为模板,在文件中寻找匹配模板的行。注意,必须使用双引号重复作用的元字符, ; 用来做标签的元字符,(); 用来锚定单词的元字符二、实例数据文件rootfuture tmp# cat newbo1 aa2 AA3 Aa45rootfuture tmp# cat bo 12sss bo-345.8 shell 5 56hello grep 5rootfuture root# grep -n bo1:12:23:sss bo-4:35:46:5.8 shell 5 57:68:hello grep 5rootfuture tmp# grep -i a newbo -i关闭大小写敏感1 aa2 AA3 Aarootfuture tmp# grep -l 2 * -l 打印匹配的文件名,而不打印匹配的行bonewborootfuture tmp# grep -c a newbo 打印有多少匹配行2rootfuture tmp# grep -c -i a newbo3rootfuture tmp# grep -w shell bo -w打印按照单词方式匹配模板的行,而不是作为单词的一部分匹配模板的行5.8 shell 5 5rootfuture tmp# grep -w hell borootfuture tmp#rootfuture tmp# grep -2 3 newbo 上下各两行1 aa2 AA3 Aa45rootfuture tmp# grep -A2 3 newbo 下两行3 Aa45rootfuture tmp# grep -B2 3 newbo 上两行1 aa2 AA3 Aarootfuture tmp# grep -C2 3 newbo 上下各两行1 aa2 AA3 Aa45rootfuture tmp# rootfuture tmp# grep -b newbo 打印每一行前打印字符偏移量(不明白具体指的是什么感觉比较抽象,在次请高人指教)0:1 aa6:2 AA12:3 Aa18:420:5rootfuture tmp# cat regular 12rootfuture tmp# grep -f regular newbo-f 是指从文件中读取模板1 aa2 AArootfuture tmp# rootfuture tmp# grep *bo:1bo:2bo:sss bo-bo:3bo:4bo:5.8 shell 5 5bo:6bo:hello grep 5newbo:1 aanewbo:2 AAnewbo:3 Aanewbo:4newbo:5regular:1regular:2rootfuture tmp# grep -h * -h 使得grep不打印头信息,这个例子中是不打印文件名12sss bo-345.8 shell 5 56hello grep 51 aa2 AA3 Aa4512rootfuture tmp# grep -q newbo -q取消grep的所有输出,在只需要退出状态值的场合这个选项就显得非常有用rootfuture tmp# echo $?0rootfuture tmp# 数据文件rootfuture root# cat bo12sss bo-345.8 shell 5 56hello grep 5rootfuture root# cat nu1ss -bo2xx bo-222222222234rootfuture root# grep 2 * 在所有的文件中搜索“2”bo:2nu:2nu:2222222222rootfuture root# grep 5 * 锚定行的开始bo:5.8 shell 5 5rootfuture root# grep 5$ bo $锚定行的结尾5.8 shell 5 5hello grep 5rootfuture root# grep 5. bo 第一个字符是5,然后紧跟着一个. 尔后是一个任意字符5.8 shell 5 5如果点前面有一个反斜杠,则点就不再特殊,而是仅仅表示一个点。rootfuture root# grep 23 bo 表示开头是以2或3开头的行23rootfuture root# grep 0-9 bo 打印所有包含非数字字符的行sss bo-5.8 shell 5 5hello grep 5rootfuture root# grep 0-9 bosss bo-5.8 shell 5 5hello grep 5rootfuture root# grep 0-9 bosss bo-5.8 shell 5 5hello grep 5rootfuture root# grep 0-9 bo 打印所有不是以数字开头的行sss bo-hello grep 5rootfuture root# grep a-z5 bo 打印每个字符串至少有5个连续小写字母的字符串的行5.8 shell 5 5hello grep 5rootfuture root# grep a-z5 bo打印开头字符串至少有5个连续小写字母的字符串的行hello grep 5rootfuture root# grep (5).0-9.*1 *1 bo 打印第一个字符是5,紧跟着一个句点,然后是任意一个数字,然后是任意字符,然后是一个5,然后是一个任意个制表符,然后又是一个5。5.8 shell 5 5rootfuture root# grep (5).0-9 1 *1 bo 之所以没有搜索到是因为没有“.*”任意字符rootfuture root# grep (5).0-9 1 .*1 borootfuture root# grep (5).0-9 .* 1 *1 bo5.8 shell 5 51 是一个引用,含义是正则表达式中第一个被(和)括起来的部分rootfuture root# grep bo 锚定单词的结尾5.8 shell 5 5hello grep 5rootfuture root# grep borootfuture root# grep bo 打印所有包含单词hello的行hello grep 5rootfuture root# grep hello bo hello grep 5rootfuture root# grep hellob bo b是单词分界符 没有理解这个参数请路过的高人注解hello grep 5rootfuture root# grep hw*w bo 也没有理解w参数,仍然请过路高人注解,万分感激!hello grep 5rootfuture root# grep bo .* 代表任意字符sss bo-hello grep 5rootfuture root# grep bo 重复4次,加上前面一次,共5次hello grep 5rootfuture root# grep borootfuture root# grep bohello grep 5rootfuture root# grep borootfuture root# grep borootfuture root# rootfuture root# grep bo 重复4次hello grep 5rootfuture root# grep bo hello grep 5rootfuture root# grep borootfuture root#注:ax,y 意义是重复a(x和y之间含x和y) rootfuture root# grep 2|3 bo grep不支持扩展的正则表达式,竖线是用于表示或的扩展正则表达式元字符。所以没有输出。rootfuture root# grep 2|3 bo 加上反斜杠,这个字符就被翻译成扩展正则表达式。23rootfuture root# grep (2|3) bo23rootfuture root# grep (5).8.*1 bo5.8 shell 5 5正则表达式5.8被匹配,模板5 就被存储到内存中的寄存器1内,这个正则表达式的含义是如果5.8被找到,标记并保存5,然后搜索任意个字符(.*),尔后是一个1代表5rootfuture root# grep (5).*1 bo5.8 shell 5 5roottest-linux tmp# cat teadlkf adfkl aa 566.5dfad 234.43 aaa234 aaaaadf adfa adf adf 45.556 aaaaaroottest-linux tmp#roottest-linux tmp#roottest-linux tmp#roottest-linux tmp# grep baaab te b 单词分界符dfad 234.43 aaaroottest-linux tmp#roottest-linux tmp# cat teadlkf adfkl aa 566.5dfad. 23

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论