Linux grep命令

来源:互联网 发布:静态构造函数数据成员 编辑:程序博客网 时间:2024/04/29 20:48

一、简介
grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的扩展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。
二、grep常用用法

# grep [-acinv] [--color=auto] '搜寻字符串' filename
选项与参数:
-a:(ascill)将 binary 文件以 text 文件的方式搜寻数据
-c: (count)计算找到 '搜寻字符串' 的次数
-i :(ignore)忽略大小写的不同,所以大小写视为相同
-n:(number)顺便输出行号
-v:(invert)反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行

-l(file)显示匹配的文件不显示匹配的行
-r:(recursive)递归查找当前目录和它的子目录的文件

-E(extend)使用扩展的正则表达式

-Ax(after)输出指定的字符串和它后面的x行

-Bx(before)输出指定的字符串和它之前的x行
--color=auto :可以将找到的关键词部分加上颜色的显示喔!

三、常用用法举例

(1)查找指定文件中的包含指定字符串的行

# grep  root /etc/passwd

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

# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

(2)查找指定文件中的包含指定字符串的行并打印出行号

# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
30:operator:x:11:0:operator:/root:/sbin/nologin

 在关键字的显示方面,grep 可以使用 --color=auto 来将关键字部分使用颜色显示。 这可是个很不错的功能啊!但是如果每次使用 grep 都得要自行加上 --color=auto 又显的很麻烦~ 此时那个好用的 alias 就得来处理一下啦!你可以在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来立即生效即可喔! 这样每次运行 grep 他都会自动帮你加上颜色显示啦

(3)查找指定文件中的不包含指定字符串的行

# grep -v root /etc/passwd | grep -v nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
mysql:x:27:482:MySQL Server:/var/lib/mysql:/bin/bash
hongwazi:x:500:500:hongwazi:/home/hongwazi:/bin/bash

(4)查找命令输出结果中包含指定字符串的行

[root@www ~]# dmesg | grep -n --color=auto 'eth'
247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10
248:eth0: Identified 8139 chip type 'RTL-8139C'
294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1
305:eth0: no IPv6 routers present
# 你会发现除了 eth 会有特殊颜色来表示之外,最前面还有行号喔!
在关键字的显示方面,grep 可以使用 --color=auto 来将关键字部分使用颜色显示。 这可是个很不错的功能啊!但是如果每次使用 grep 都得要自行加上 --color=auto 又显的很麻烦~ 此时那个好用的 alias 就得来处理一下啦!你可以在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来立即生效即可喔! 这样每次运行 grep 他都会自动帮你加上颜色显示啦

 (5)输出指定字符串附近指定行的内容
[root@www ~]# dmesg | grep -n -A3 -B2 --color=auto 'eth'
245-PCI: setting IRQ 10 as level-triggered
246-ACPI: PCI Interrupt 0000:00:0e.0[A] -> Link [LNKB] ...
247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10
248:eth0: Identified 8139 chip type 'RTL-8139C'
249-input: PC Speaker as /class/input/input2
250-ACPI: PCI Interrupt 0000:00:01.4[B] -> Link [LNKB] ...
251-hdb: ATAPI 48X DVD-ROM DVD-R-RAM CD-R/RW drive, 2048kB Cache, UDMA(66)
# 如上所示,你会发现关键字 247 所在的前两行及 248 后三行也都被显示出来!
# 这样可以让你将关键字前后数据捉出来进行分析啦!

 (6)递归查找目录

# grep     ‘energywise’    *         #在当前目录搜索带'energywise'行的文件

# grep -r    ‘energywise’ *         #在当前目录及其子目录下搜索'energywise'行的文件
# grep -l -r ‘energywise’ *         #在当前目录及其子目录下搜索'energywise'行的文件,但是不显示匹配的行,只显示匹配的文件
 四、grep与正规表达式

(1)[ ]匹配里面一个字符
#grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.

其实 [] 里面不论有几个字节,他都谨代表某『一个』字节, 所以需要查找的字串是『tast』或『test』

(2)字符类的反向选择 [^]

#grep -n '[^g]oo' regular_express.txt #匹配含有不是以g开头的但是以oo结尾的字符串的行

2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!
 

# grep -n '[^a-z]oo' regular_express.txt#匹配含有以大写字母开始并且oo结尾的字符串的行
3:Football game is not use feet only.
也就是说,当我们在一组集合字节中,如果该字节组是连续的,例如大写英文/小写英文/数字等等, 就可以使用[a-z],[A-Z],[0-9]等方式来书写,那么如果我们的要求字串是数字与英文呢? 呵呵!就将他全部写在一起,变成:[a-zA-Z0-9]。

#grep -n '[0-9]' regular_express.txt #匹配一行有数字的行
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
 

(3)行首与行尾字节 ^ $
#grep -n '^the' regular_express.txt      #匹配以the开头的行
12:the symbol '*' is represented as start.
 

# grep -n '^[a-z]' regular_express.txt   #匹配以小写字母开头的行
2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.

 

# grep -n '^[^a-zA-Z]' regular_express.txt  #匹配不是以字母开始的行
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird
^ 符号,在字符类符号(括号[ ])之内与之外是不同的。 [ ]『反向选择』,在[ ] 之外则代表定位在行首的意义!

 # grep -n '\.$' regular_express.txt  #匹配以“.”结尾的行
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
11:This window is clear.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
20:go! go! Let's go.

特别注意到,因为小数点具有其他意义(底下会介绍),所以必须要使用转义字符(\)来加以解除其特殊意义!

# grep -n '^$' regular_express.txt  #找出空白行
22:
(4) 任意一个字节 .        重复字节 *
. (小数点):代表『一定有一个任意字节』的意思;
* (星号):代表『重复前一个字符, 0 到无穷多次』的意思,为组合形态
# grep -n 'g..d' regular_express.txt  #匹配四个字符的字符串,其中第一个字符是g,第四个字符是d,中间两个为任意字符
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".
 

# grep -n 'ooo*' regular_express.txt  #匹配两个oo及以上的字符串
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!

  

# grep -n 'g.*g' regular_express.txt     #匹配g开始和g结尾的字符串的行,中间字符可有可无
1:"Open Source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.

 

[root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt  #匹配数字位数至少为1的行
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
 

(5)限定连续 RE 字符范围 {}

利用 . 与 RE 字符及 * 来配置 0 个到无限多个重复字节, 那如果要限制一个范围区间内的重复字节数呢?

就得要使用到限定范围的字符 {} 了。 但因为 { 与 } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用字符   \ 来转义。


# grep -n 'o\{2\}' regular_express.txt      #匹配包含有两个o的字符串的行
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search ke
19:goooooogle yes!

 

 # grep -n 'go\{2,5\}g' regular_express.txt  #匹配字符串,以go开始g结尾中间可以有2到5个的o
18:google is the best tools for search keyword.
 

# grep -n 'go\{2,\}g' regular_express.txt     #匹配字符串,以go开始g结尾中间可以有2到无数个o
18:google is the best tools for search keyword.
19:goooooogle yes!
 

(6)扩展grep -E

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

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

#grep -E 'NW|EA' testfile   #匹配含有NW或者EA的字符串
northwest       NW      Charles Main        3.0     .98     3       34
eastern         EA      TB Savage           4.4     .84     5       20
 

# grep -E '3+' testfile   #匹配包含一个或者多个3的行   

# grep -E '(no)+' testfile #搜索一个或者多个连续的no的行
 

(7)不使用正则表达式

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

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

fgrep  '*' /etc/profile
for i in /etc/profile.d/*.sh ; do


grep -F '*' /etc/profile
for i in /etc/profile.d/*.sh ; do

参考文献:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html

原创粉丝点击