前面学习的grep都是最基本的unix GREP语法,但是LINUX GREP是GNU GREP,基本语法相同,但是扩展了这些语法,特别是正则表达式,LINUX 在grep命令选项中可以通过一些参数来确定不同的正则表达式格式。

 

image

从上面表达式中可以看出,实际上如grep –E 就相当于egrep, grep –F 就相当于fgrep,最后一种是perl 正则,这个比较特殊一点。Linux Grep缺省情况下比GNU Grep会支持如下三种正则表达式:

\w词字符标志,也就是大小写加上数字和下划线|\w*e在字符|后面跟上零到多个词字符,并在后面跟上e如love, le,l9e
\W非词字符标志,也就是除大小写数字及下划线之外\W\w*匹配一个非词字符,并且后面跟上零或者多个词字符
\b字边界等于\< \>,也等于-w 选项\blove\b匹配单词love

[windriver@windriver-machine ltest]$ grep '^b\w*\W' datafile
[windriver@windriver-machine ltest]$ grep '^n\w*\W' datafile
northwest  NW Charles Main 3.0 .90 3 34
northeast  NE AM Main Jr. 5.1  .94 3 13
north      NO Margot Weber 4.5 .89 5 9
[windriver@windriver-machine ltest]$ grep '^[a-z]\w*\W' datafile
northwest  NW Charles Main 3.0 .90 3 34
western    WE Sharon Gray  5.3 .97 5 23
southwest  SW Lewis Dalsass 2.7 .8 2 18
southern   SO Suan chin    5.1 .95 4 15
southeast  SE Patricia    4.0  .7  4 17
eastern    EA TB Savage   4.4  .84 5 20
northeast  NE AM Main Jr. 5.1  .94 3 13
north      NO Margot Weber 4.5 .89 5 9
central    CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ grep '\bnorth\b' datafile
north      NO Margot Weber 4.5 .89 5 9
[windriver@windriver-machine ltest]$ grep '\<north\>' datafile
north      NO Margot Weber 4.5 .89 5 9
[windriver@windriver-machine ltest]$

[windriver@windriver-machine ltest]$ grep 'NW|EA' datafile
[windriver@windriver-machine ltest]$ egrep 'NW|EA' datafile
northwest  NW Charles Main 3.0 .90 3 34
eastern    EA TB Savage   4.4  .84 5 20
[windriver@windriver-machine ltest]$ grep -E 'NW|EA' datafile
northwest  NW Charles Main 3.0 .90 3 34
eastern    EA TB Savage   4.4  .84 5 20
[windriver@windriver-machine ltest]$ grep -E 'NW\|EA' datafile
[windriver@windriver-machine ltest]$ grep  'NW\|EA' datafile
northwest  NW Charles Main 3.0 .90 3 34
eastern    EA TB Savage   4.4  .84 5 20
[windriver@windriver-machine ltest]$ grep -E '3+' datafile
northwest  NW Charles Main 3.0 .90 3 34
western    WE Sharon Gray  5.3 .97 5 23
northeast  NE AM Main Jr. 5.1  .94 3 13
central    CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ egrep '3+' datafile
northwest  NW Charles Main 3.0 .90 3 34
western    WE Sharon Gray  5.3 .97 5 23
northeast  NE AM Main Jr. 5.1  .94 3 13
central    CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ grep '3\+' datafile
northwest  NW Charles Main 3.0 .90 3 34
western    WE Sharon Gray  5.3 .97 5 23
northeast  NE AM Main Jr. 5.1  .94 3 13
central    CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ egrep '2\.?[0-9]' datafile
western    WE Sharon Gray  5.3 .97 5 23
southwest  SW Lewis Dalsass 2.7 .8 2 18
eastern    EA TB Savage   4.4  .84 5 20
[windriver@windriver-machine ltest]$

从上面我们还可以看出一个功能,就是Egrep扩展的一些的元字符,如用grep时如何用这些元字符呢,可以使用\进行转义,一下子就变成了EGREP,相当于grep –E.

grep 还有一个选项是遍历文件目录,grep –r ,有些LINUX提供了rgrep。还有一些其它选项,可以使用man来提示。

[windriver@windriver-machine ltest]$ grep -V
grep (GNU grep) 2.5.1

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[windriver@windriver-machine ltest]$ man grep
[windriver@windriver-machine ltest]$ man grep
[windriver@windriver-machine ltest]$ grep -A datafile
grep: datafile: invalid context length argument

[windriver@windriver-machine ltest]$ grep -A 'Nw' datafile
grep: Nw: invalid context length argument

[windriver@windriver-machine ltest]$ grep -A 2 'NW' datafile
northwest  NW Charles Main 3.0 .90 3 34
western    WE Sharon Gray  5.3 .97 5 23
southwest  SW Lewis Dalsass 2.7 .8 2 18
[windriver@windriver-machine ltest]$ grep -A 1 'NW' datafile
northwest  NW Charles Main 3.0 .90 3 34
western    WE Sharon Gray  5.3 .97 5 23
[windriver@windriver-machine ltest]$ grep -A 0 'NW' datafile
northwest  NW Charles Main 3.0 .90 3 34
[windriver@windriver-machine ltest]$ grep -B 0 'NW' datafile
northwest  NW Charles Main 3.0 .90 3 34
[windriver@windriver-machine ltest]$ grep -B 1 'NW' datafile
northwest  NW Charles Main 3.0 .90 3 34
[windriver@windriver-machine ltest]$ man grep
[windriver@windriver-machine ltest]$