码迷,mamicode.com
首页 > 其他好文 > 详细

grep和rgrep和fgrep

时间:2017-02-14 17:46:29      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:ever   需要   不同   ack   表示   使用   not   功能   put   

Linux环境下。

(1)grep对标签元字符的表示。

[berry@berry:practice] grep ‘w\(es\).*\1‘ text
northwest NW Charles Main 3.0 .98 3 34
[berry@berry:practice] grep -E ‘w\(es\).*\1‘ text
grep: Invalid back reference
[berry@berry:practice] grep -E ‘w(es).*\1‘ text
northwest NW Charles Main 3.0 .98 3 34
[berry@berry:practice] egrep  ‘w(es).*\1‘ text
northwest NW Charles Main 3.0 .98 3 34

(2)grep对锚定单词的元字符的表示。

[berry@berry:practice] grep ‘\<no\>‘ text 
no one 
no no123 
[berry@berry:practice] grep -E ‘\<no\>‘ text 
no one 
no no123 
[berry@berry:practice] egrep  ‘\<no\>‘ text 
no one 
no no123 

(3)对重复作用的元字符的表示。

[berry@berry:practice] grep ‘\.[0-9]\{2\}[^0-9]‘ text
northwest NW Charles Main 3.0 .98 3 34
northwest NW Charles Main 3.0 .98 3 34
[berry@berry:practice] grep -E ‘\.[0-9]{2}[^0-9]‘ text
northwest NW Charles Main 3.0 .98 3 34
northwest NW Charles Main 3.0 .98 3 34
[berry@berry:practice] egrep ‘\.[0-9]{2}[^0-9]‘ text
northwest NW Charles Main 3.0 .98 3 34
northwest NW Charles Main 3.0 .98 3 34

 (4)Unix环境下,(Solaris egrep)

# egrep ‘\.[0-9]{2}[^0-9]‘ datafile
<no output; not recognized with or without backslashes>

 说明
1 扩展元字符{}用来表示重复。除非在前面加上反斜杠,否则Gnu 和UNIX 版本的正规grep 无法
识别这个扩展元字符。该正则表达式的意思是:搜索这样的行,该行包含这样的字符串,第一个
字符是一个点号\.,紧跟着一个0~9之间的数字[0-9],如果该字符串重复两次\{2\},则后面就是
一个非数字的字符[^0-9]。
2 若使用扩展grep或者grep -E选项,则在表示重复的元字符{2}的括号前面就不需要加反斜杠了。
3 因为Gnu 版本的egrep 跟grep -E的功能一样,因此这个例子的结果跟前面相同。
4 这是标准的UNIX下的egrep,不论是否有反斜杠它都无法识别花括号元字符{}。

 

(5)rgrep的使用,跟其他grep家族成员不同,rgrep 可以递归访问目录树。rgrep 有一些命令行选项支持跟
标准的grep 一样的选项。

[berry@berry:practice] grep "hello" text
adfdfdfdfddhellodffdf  
adfdfdfdfddhellodffdf  
adfdfdfdfddhellodffdf  
adfdfdfdfddhellodffdf  
adfdfdfdfddhellodffdf  
hello word
hello word
hello berry!Good morning,every one!
hello berry!Good morning,every one!
hello berry!Good morning,every one!
hello word
hello berry!Good morning,every one!
hello berry!Good morning,every one!
hello word
hello berry!Good morning,every one!
hello word
hello berry!Good morning,every one!
[berry@berry:practice] rgrep "hello" .
./b.txt:A hello world
./text:adfdfdfdfddhellodffdf  
./text:adfdfdfdfddhellodffdf  
./text:adfdfdfdfddhellodffdf  
./text:adfdfdfdfddhellodffdf  
./text:adfdfdfdfddhellodffdf  
./text:hello word
./text:hello word
./text:hello berry!Good morning,every one!
./text:hello berry!Good morning,every one!
./text:hello berry!Good morning,every one!
./text:hello word
./text:hello berry!Good morning,every one!
./text:hello berry!Good morning,every one!
./text:hello word
./text:hello berry!Good morning,every one!
./text:hello word
./text:hello berry!Good morning,every one!
./dir1/a.txt:hello

 

(6)fgrep 的命令行形式跟grep 相似,但是它不识别任何正则表达式元字符。所有的字符都
表示它们自己。一个插入符号就表示插入符号,一个美元符号就表示美元符号等等。-F 选项
使得Gnu grep 精确地模仿fgrep。

[berry@berry:practice] fgrep ‘[A-Z]****[0-9]..$5.00‘ text
[A-Z]****[0-9]..$5.00
[berry@berry:practice] grep -F ‘[A-Z]****[0-9]..$5.00‘ text
[A-Z]****[0-9]..$5.00

 

(7)grep -w no text 和grep ‘\<no\>‘ text的效果是一样的,都是表示按照单词方式匹配,而不是只是作为

单词的一部分进行匹配。

[berry@berry:practice] grep ‘\<no\>‘ text
no one 
no one 
no no123 
[berry@berry:practice] grep ‘no‘ text
no one 
no one 
no no123 
northwest NW Charles Main 3.0 .98 3 34
northwest NW Charles Main 3.0 .98 3 34
noone
[berry@berry:practice] grep -w "no" text
no one 
no one 
no no123 

 

grep和rgrep和fgrep

标签:ever   需要   不同   ack   表示   使用   not   功能   put   

原文地址:http://www.cnblogs.com/Berryxiong/p/6398535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!