grep用法详解.doc_第1页
grep用法详解.doc_第2页
grep用法详解.doc_第3页
grep用法详解.doc_第4页
grep用法详解.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

grep用法详解:grep与正则表达式首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串。vim、grep、awk 、sed 都支持正则表达式,也正是因为由于它们支持正则,才显得它们强大;在以前上班的公司里,由于公司是基于web的服务型网站(nginx),对正则的需求比 较大,所以也花了点时间研究正则,特与大家分享下:1基础正则表达式grep 工具,以前介绍过。grep -acinv 搜索内容串 filename-a 以文本文件方式搜索-c 计算找到的符合行的次数-i 忽略大小写-n 顺便输出行号-v 反向选择,即找 没有搜索字符串的行其中搜索串可以是正则表达式!1搜索有the的行,并输出行号$grep -n the regular_express.txt搜 索没有the的行,并输出行号$grep -nv the regular_express.txt2 利 用搜索集合字符 表示其中的某一个字符 ,例如ade 表示a或d或ewoodyxiaoc:/tmp$ grep -n taest regular_express.txt 8:I cant finish the test.9:Oh! the soup taste good!可以用符号做内的前缀,表示除内的字符之外的字 符。比如搜索oo前没有g的字符串所在的行. 使用 goo 作搜索字符串woodyxiaoc:/tmp$ grep -n goo regular_express.txt 2:apple is my favorite food.3:Football game is not use feet only.18:google is the best tools for search keyword.19:goooooogle yes! 内可以用范围表示,比如a-z 表示小写字母,0-9 表示09的数字, A-Z 则是大写字母们。a-zA-Z0-9表示所有数字与英文字符。 当然也可以配合来排除字符。搜索包含数字的行woodyxiaoc:/tmp$ grep -n 0-9 regular_express.txt 5:However ,this dress is about $ 3183 dollars.15:You are the best is menu you are the no.1.行首与行尾字符 $. 表示行的开头,$表示行的结尾( 不是字符,是位置)那么$ 就表示空行,因为只有行首和行尾。这里与里面使用的意义不同。它表示后面的串是在行的开头。比如搜索the在开头的行woodyxiaoc:/tmp$ grep -n the regular_express.txt 12:the symbol * is represented as star.搜索以小写字母开头的行woodyxiaoc:/tmp$ grep -n a-z regular_express.txt 2:apple is my favorite food.4:this dress doesnt fit me.10:motorcycle is cheap than car.12:the symbol * is represented as star.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Lets go.woodyxiaoc:/tmp$ 搜索开头不是英文字母的行woodyxiaoc:/tmp$ grep -n a-zA-Z regular_express.txt 1:Open Source is a good mechanism to develop programs.21:#I am VBirdwoodyxiaoc:/tmp$ $表示它前面的串是在行的结尾,比如 . 表示 . 在一行的结尾搜索末尾是.的行woodyxiaoc:/tmp$ grep -n .$ regular_express.txt /. 是正则表达式的特殊符号,所以要用转义1:Open Source is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.4:this dress doesnt fit me.5:However ,this dress is about $ 3183 dollars.6:GNU is free air not free beer.注意在MS的系统下生成的文本文件,换行会加上一个 M 字符。所以最后的字符会是隐藏的M ,在处理Windows下面的文本时要特别注意!可以用cat dos_file | tr -d r unix_file 来删除M符号。 M=r那么$ 就表示只有行首行尾的空行拉!搜索空行woodyxiaoc:/tmp$ grep -n $ regular_express.txt 22:23:woodyxiaoc:/tmp$ 搜索非空行woodyxiaoc:/tmp$ grep -vn $ regular_express.txt 1:Open Source is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.4:this dress doesnt fit me.任意一个字符. 与重复字符 *在bash中*代表通配符,用来代表任意个 字符,但是在正则表达式中,他含义不同,*表示有0个或多个 某个字符。例如 oo*, 表示第一个o一定存在,第二个o可以有一个或多个,也可以没有,因此代表至少一个o.点. 代表一个任意字符,必须存在。 g?d 可以用 g.d 表示。 good ,gxxd ,gabd .都符合。woodyxiaoc:/tmp$ grep -n g.d regular_express.txt 1:Open Source is a good mechanism to develop programs.9:Oh! the soup taste good!16:The world is the same with glad.woodyxiaoc:/tmp$ 搜索两个o以上的字符串woodyxiaoc:/tmp$ grep -n ooo* regular_express.txt /前两个o一定存在,第三个o可没有,也可有多个。1:Open Source is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! the soup taste good!18:google is the best tools for search keyword.19:goooooogle yes!搜索g开头和结尾,中间是至少一个o的字符串,即gog, goog.gooog.等woodyxiaoc:/tmp$ grep -n goo*g regular_express.txt 18:google is the best tools for search keyword.19:goooooogle yes!搜索g开头和结尾的字符串在的行woodyxiaoc:/tmp$ grep -n g.*g regular_express.txt / .*表示 0个或多个任意字符1:Open Source is a good mechanism to develop programs.14:The gd software is a library for drafting programs.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Lets go.限定连续重复字符的范围 . * 只能限制0个或多个, 如果要确切的限制字符重复数量,就用范围 。范围是数字用,隔开 2,5 表示25个,2表示2个,2, 表示2到更多个注意,由于 在SHELL中有特殊意义,因此作为正则表达式用的时候要用转义一下。搜索包含两个o的字符串的行。woodyxiaoc:/tmp$ grep -n o2 regular_express.txt 1:Open Source is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! the soup taste good!18:google is the best tools for search keyword.19:goooooogle yes!搜索g后面跟25个o,后面再跟一个g的字符串的行。woodyxiaoc:/tmp$ grep -n go2,5g regular_express.txt 18:google is the best tools for search keyword.搜索包含g后面跟2个以上o,后面再跟g的行。woodyxiaoc:/tmp$ grep -n go2,g regular_express.txt 18:google is the best tools for search keyword.19:goooooogle yes!注意,相让中的 不表现特殊意义,可以放在里面内容的后面。a-z.! - 表示没有小写字母,没有. 没有!, 没有空格,没有- 的 串,注意里面有个小空格。另外shell 里面的反向选择为!range, 正则里面是 range2扩展正则表达式扩展正则表达式是对基础正则表达式添加了几个特殊构成的。它令某些操作更加方便。比如我们要去除 空白行和行首为 #的行, 会这样用:woodyxiaoc:/tmp$ grep -v $ regular_express.txt | grep -v #Open Source is a good mechanism to develop programs.apple is my favorite food.Football game is not use feet only.this dress doesnt fit me.然而使用支持扩展正则表达式的 egrep 与扩展特殊符号 | ,会方便许多。注意grep只支持基础表达式, 而egrep 支持扩展的, 其实 egrep 是 grep -E 的别名而已。因此grep -E 支持扩展正则。那么:woodyxiaoc:/tmp$ egrep -v $|# regular_express.txt Open Source is a good mechanism to develop programs.apple is my favorite food.Football game is not use feet only.this dress doesnt

温馨提示

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

评论

0/150

提交评论