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