参考链接:

http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html


grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。

egrep是grep的扩展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。

linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。


grep 常用用法:

-a :将 binary 文件以 text 文件的方式搜寻数据

-c :计算找到 '搜寻字符串' 的次数

-i :忽略大小写的不同,所以大小写视为相同

-n :顺便输出行号

-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!

--color=auto :可以将找到的关键词部分加上颜色的显示喔!


eg:

将/etc/passwd,有出现 root 的行取出来

[root@open*** ~]# grep root /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin


将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号

[root@open*** ~]# grep -n root /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

11:operator:x:11:0:operator:/root:/sbin/nologin


显示关键字的后三行与前两行

[root@open*** ~]# dmesg |grep -n -A3 -B2 'eth'

476-udev: starting version 147

477-piix4_smbus 0000:00:01.3: SMBus base address uninitialized - upgrade BIOS or use force_addr=0xaddr

478:Initialising Xen virtual ethernet driver.

479-  alloc irq_desc for 928 on node 0

480-  alloc kstat_irqs on node 0

481-  alloc irq_desc for 927 on node 0


grep tar *        在当前目录下搜索带tar的行,显示行及文件

grep -r tar *     在当前目录及其子目录下搜索带tar的行,显示行及文件

grep -l -r tar * 在当前目录及其子目录下搜索带tar的行的文件,但是不显示匹配的行,只显示匹配的文件


grep与正则表达式

[] 里面不论有几个字节,他都谨代表某『一个』字节

[root@open*** ~]# grep -n "t[ae]st" agc.txt     #只能匹配a或e,不能同时匹配ae

4:I can't finish the test.

5:Oh! The soup taste good.


字符类的反向选择 [^] 想要搜索到有 oo 的行,但不想要 oo 前面有 g

[root@open*** ~]# grep -n [^g]oo agc.txt

7:apple is my favorite food.

8:Football game is not use feet only.

9:google is the best tools for search keyword.   #有一个too

10:goooooogle yes!                               #这个oo前面还有o


oo 前面不想要有小写/大写/数字 字符

grep -n [^a-zA-Z0-9]oo agc.txt


想取得有数字的那一行

[root@open*** ~]# grep -n [0-9] agc.txt 

6:Oh! The soup teaste good 9.


行首与行尾字节 ^ $

只列出只有the开头的行

[root@open*** ~]# grep -n "^the" agc.txt 

5:the soup taste good.

6:the soup teaste good 9.


只列出大写字符开头的行

[root@open*** ~]# grep -n "^[A-Z]" agc.txt       

4:I can't finish the test.

8:Football game is not use feet only.


若要列出不是英文字母开头的行

[root@open*** ~]# grep -n "^[^a-zA-Z]" agc.txt #字符类的反向选择 [^] 

1:9tar 


取出以?结尾的行

[root@open*** ~]# grep -n "?$" agc.txt           

2:ddddddddddddd?


取出以.结尾的行    因为小数点有特殊意义,所以需要转义符转义

[root@open*** ~]# grep -n "\.$" agc.txt     

4:I can't finish the test.

8:Football game is not use feet only.

9:google is the best tools for search keyword.


找出空白行

[root@open*** ~]# grep -n "^$" agc.txt 

8:


任意一个字节 . 与重复字节 *

. (小数点):代表『一定有一个任意字节』的意思;

* (星号):代表『重复前一个字符, 0 到无穷多次』的意思,为组合形态


找出 g??d 的字串

[root@open*** ~]# grep -n "g..d" agc.txt 

5:the soup taste good

6:the soup teaste good 9


想要列出有 oo, ooo, oooo 等等的数据

分析:

* 代表的是『重复 0 个或多个前面的 RE 字符』的意义, 因此,『o*』代表的是:『拥有空字节或一个 o 以上的字节』,

因此,『 grep -n 'o*' regular_express.txt 』将会把所有的数据都列印出来终端上!

所以,当我们需要『至少两个 o 以上的字串』时,就需要 ooo* 

[root@open*** ~]# grep -n "ooo*" agc.txt 

5:the soup taste good

6:the soup teaste good 9

7:apple is my favorite foo

9:Football game is not use feet only.

10:google is the best tools for search keyword.

11:goooooogle yes!


想要字串开头与结尾都是 g,但是两个 g 之间仅能存在至少一个 o ,亦即是 gog, goog, gooog.... 

[root@open*** ~]# grep -n "goo*g" agc.txt   

10:google is the best tools for search keyword.

11:goooooogle yes!


找出 g 开头与 g 结尾的行,当中的字符可有可无

[root@open*** ~]# grep -n "g.*g" agc.txt      # .* 的 RE 表示任意字符是很常见的

8:gg

10:google is the best tools for search keyword.

11:goooooogle yes!


找出『任意数字』的行,因为仅有数字,所以

[root@open*** ~]# grep -n "[0-9][0-9]*" agc.txt 

1:9tar 

6:the soup teaste good 9


限定连续 RE 字符范围 {}

举例来说,我想要找出两个到五个 o 的连续字串,该如何作?这时候就得要使用到限定范围的字符 {} 了。但因为 { 与 } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用字符   \ 来让他失去特殊意义才行。


{} 的语法是这样的,要找到两个 o 的字串

[root@open*** ~]# grep -n "o\{2\}" agc.txt  

5:the soup taste good

6:the soup teaste good 9

7:apple is my favorite foo

9:Football game is not use feet only.

10:google is the best tools for search keyword.

11:goooooogle yes!


要找出 g 后面接 2 到 6 个 o ,然后再接一个 g 的字串

[root@open*** ~]# grep -n "go\{2,6\}g" agc.txt  

10:google is the best tools for search keyword.

11:goooooogle yes!


想要的是 2 个 o 以上的 goooo....g 

[root@open*** ~]# grep -n "go\{2,\}g" agc.txt  

10:google is the best tools for search keyword.

11:goooooogle yes!


扩展grep(grep -E 或者 egrep):

使用扩展grep的主要好处是增加了额外的正则表达式元字符集。


打印所有包含NW或EA的行

[root@open*** ~]# egrep "NW|EA" agc.txt

northwest       NW      Charles Main        3.0     .98     3       34

eastern         EA      TB Savage           4.4     .84     5       20

[root@open*** ~]# grep -E "NW|EA" agc.txt 

northwest       NW      Charles Main        3.0     .98     3       34

eastern         EA      TB Savage           4.4     .84     5       20


对于标准grep,如果在扩展元字符前面加\,grep会自动启用扩展选项-E。

[root@open*** ~]# grep "NW\|EA" agc.txt 

northwest       NW      Charles Main        3.0     .98     3       34

eastern         EA      TB Savage           4.4     .84     5       20


搜索所有包含一个或多个3的行

[root@open*** ~]# grep "3\+" agc.txt 

the soup teaste good 93

northwest       NW      Charles Main        3.0     .98     3       34

[root@open*** ~]# egrep "3+" agc.txt  

the soup teaste good 93

northwest       NW      Charles Main        3.0     .98     3       34

[root@open*** ~]# grep -E "3+" agc.txt  

the soup teaste good 93

northwest       NW      Charles Main        3.0     .98     3       34


搜索一个或者多个连续的no的行

[root@open*** ~]# egrep "(no)+" agc.txt      

ggnono

adbcnoinonodd

northwest       NW      Charles Main        3.0     ..98     3       34

Football game is not use feet only.

[root@open*** ~]# grep -E "(no)+" agc.txt 

ggnono

adbcnoinonodd

northwest       NW      Charles Main        3.0     ..98     3       34

Football game is not use feet only.

[root@open*** ~]# grep "\(no\)\+" agc.txt   

ggnono

adbcnoinonodd

northwest       NW      Charles Main        3.0     ..98     3       34

Football game is not use feet only.



不使用正则表达式

fgrep 查询速度比grep命令快,但是不够灵活:它只能找固定的文本,而不是规则表达式。

如果你想在一个文件或者输出中找到包含星号字符的行

[root@open*** ~]# grep -F "*" agc.txt 

9tar** 

**ggnono

[root@open*** ~]# fgrep "*" agc.txt 

9tar** 

**ggnono



精确匹配
[root@nginx01 tmp]# grep ad /etc/passwd       #会把包含ad字符串的都显示出来
adm:x:3:4:adm:/var/adm:/sbin/nologin
adb:x:1001:1001::/home/adb:/bin/bash
ad:x:1002:1002::/home/ad:/bin/bash

[root@nginx01 tmp]# grep -w ad /etc/passwd     #-w精确匹配ad字符串
ad:x:1002:1002::/home/ad:/bin/bash

[root@nginx01 tmp]# grep  "\<ad\>" /etc/passwd     #"\<ad\>" 精确匹配ad字符串
ad:x:1002:1002::/home/ad:/bin/bash