grep和rgrep和fgrep

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 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值