
文章目录一. 基本认识二. 基础例子1. 包含某个字符串2. 查找字符串所在位置、出现行数3. 反向匹配三. 正则例子1. 匹配行首、行尾2. 计算空行数量3. 匹配多种可能4. 反选搜索5. 匹配固定数量的任意字符6. 匹配零到无限个固定字符7. 使用扩展的正则表达式egrep一. 基本认识grep命令是一种强大的文本搜索工具它能使用正则表达式搜索文本并把匹配到的标红。grep全称是Global Regular Expression Print表示全局正则表达式版本它的使用权限是所有用户。grep的工作方式:它在一个或多个文件中逐行读取每个文件或输入流的内容并对每一行应用模式匹配搜索。搜索的结果被送到标准输出不影响原文件内容。如果要匹配多个文件文件名之间以空格分隔如grep match_pattern file_1 file_2 file_3 ...命令参数:-A显示行数除了显示符合范本样式的那一列之外并显示该行之后的内容。 -B显示行数除了显示符合样式的那一行之外并显示该行之前的内容。 -C显示行数除了显示符合样式的那一行之外并显示该行之前后的内容。 -c统计匹配的行数-e实现多个选项间的逻辑or 关系 -E扩展的正则表达式-fFILE从FILE获取PATTERN匹配-F相当于fgrep-i--ignore-case#忽略字符大小写的差别。-n显示匹配的行号 -o仅显示匹配到的字符串 -q 静默模式不输出任何信息 -s不显示错误信息。 -v显示不被pattern 匹配到的行相当于[^]反向匹配-w匹配 整个单词二. 基础例子[rootlocalhost~]# grep[-ivnc]需要匹配的字符文件名 #-i不区分大小写 #-c统计包含匹配的行数 #-n输出行号 #-v反向匹配1. 包含某个字符串#查找包含name所在行[rootlocalhost~]# grepnametomAndJerry.txt The cats name is Tom,whats the mouses name?# 忽略大小写[rootlocalhost~]# grep-inametomAndJerry.txt The cats name is Tom,whats the mouses name?The mouses Name is Jerry2. 查找字符串所在位置、出现行数#打印出含有name的行所在位置[rootlocalhost~]# grep-nnametomAndJerry.txt1:The cats name is Tom,whats the mouses name?#c:包含name的行数[rootlocalhost~]# grep-cnametomAndJerry.txt1#ci:包含name(不区分大小写)的行数[rootlocalhost~]# grep-cinametomAndJerry.txt23. 反向匹配# 打印不包含name的行[rootlocalhost~]# grep-vnametomAndJerry.txt The mouses Name is Jerry They are good friends # 打印不包含name(不区分大小写)的行[rootlocalhost~]# grep-vinametomAndJerry.txt They are good friends三. 正则例子例子文本[rootlocalhost~]# cat RegExp.txt----TEXT BEGIN----good morning teacher hello world is a script gold sunshine looks beautiful golden time flies god bless me what a delicious food they teast Good you fell glad wrong word gooood wrong word gl0d wrong word gl2d wrong word gl3d www.helloworld.com wwwhelloworldcom Uppercase100%means pure php have a gd module-----TEXT END-----1. 匹配行首、行尾#搜索以good开头的行[rootlocalhost~]# grep^goodRegExp.txt good morning teacher匹配行尾#搜索以Good结尾的行[rootlocalhost~]# grepGood$RegExp.txt they teast Good2. 计算空行数量#搜索空行的行数[rootlocalhost~]# grep-c^$RegExp.txt23. 匹配多种可能#搜索包含Good和good的行[rootlocalhost~]# grep[Gg]oodRegExp.txt good morning teacher they teast Good4. 反选搜索#搜索一个包含ood的行但是不能是Good或good #尖角号表示的是“非”[rootlocalhost~]# grep[^Gg]oodRegExp.txt what a delicious food wrong word gooood5. 匹配固定数量的任意字符#搜索包含一个词该词以g开头、紧接着是两个任意字符、再接着是一个d的行[rootlocalhost~]# grepg..dRegExp.txt good morning teacher gold sunshine looks beautiful golden time flies you fell glad wrong word gl0d wrong word gl2d wrong word gl3d #搜索包含一个词该词以G或g开头、紧接着是两个任意字符、再接着是一个d的行[rootlocalhost~]# grep[Gg]..dRegExp.txt good morning teacher gold sunshine looks beautiful golden time flies they teast Good you fell glad wrong word gl0d wrong word gl2d wrong word gl3d #搜索这样一些行该行包含某个单词该词满足如下条件 #1.第一个字符可以是G或g #2.第二个字符可以是l或o #3.第三个字符可以是换行符之外的任意字符 #4.第四个字符一定是d[rootlocalhost~]# grep[Gg][lo].dRegExp.txt good morning teacher gold sunshine looks beautiful golden time flies they teast Good you fell glad wrong word gl0d wrong word gl2d wrong word gl3d6. 匹配零到无限个固定字符#搜索这样一些行该行包含某个单词该词满足如下条件 #1.以g开头 #2.g后面跟零到无限个o #3.零到无限个o后面跟d[rootlocalhost~]# grep go*d RegExp.txt good morning teacher god bless me wrong word gooood php have a gd module7. 使用扩展的正则表达式egrep#搜索g和d之间至少有一个o的行 #“”代表匹配前面的字符1次以上含1次[rootlocalhost~]# egrepgodRegExp.txt good morning teacher god bless me wrong word gooood #搜索g和d之间只有0个或1个o的行0次或1次 #“?”代表匹配前面的字符1次以上[rootlocalhost~]# egrepgo?dRegExp.txt god bless me php have a gd module #搜索有glad或gold的行[rootlocalhost~]# egrepglad|goldRegExp.txt gold sunshine looks beautiful golden time flies you fell glad #搜索有glad或gold的行的另一种写法[rootlocalhost~]# egrepg(la|ol)dRegExp.txt gold sunshine looks beautiful golden time flies you fell glad参考《linux系统命令及Shell脚本实践指南》