既然要使用 grep 当然就得要先了解一下 grep 的语法啰~
[root@test root]# grep [-acinv] '搜寻字符串' filename
参数说明:
-a :将 binary 档案以 text 档案的方式搜寻数据
-c :计算找到 '搜寻字符串' 的次数
-i :忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!
范例:
[root@test root]# grep 'root' /var/log/secure
将 /var/log/secure 这个档案中有 root 的那一行秀出来
[root@test root]# grep -v 'root' /var/log/secure
若该行没有 root 才将数据秀出来到屏幕上!
[root@test root]# last | grep root
若该行有 root 才将数据秀出来到屏幕上! |
grep 是一个很常见也很常用的指令,他最重要的功能就是进行字符串数据的比对, 然后将符合使用者需求的字符串打印出来。 需要说明的是『grep 在资料中查寻一个字符串时,是以 "整行" 为单位来进行数据的撷取的!』也就是说,假如一个档案内有 10 行,其中有两行具有你所搜寻的字符串,则将那两行显示在屏幕上,其它的就丢弃了!
而 grep 除了可以进行档案的资料搜寻之外,也常常被应用在 input/output 的数据处理当中,例如常见的 管线命令 ( pipe ) 就可以常常见到他的踪影! 以上面表格中的例子来看,我们可以发现前两个例子是查寻档案的内容,有没有加上 -v 所显示出来的结果是『相反的!』,而第三个例子则是以 pipe 的功能进行数据的处理的喔!
好了,我们就开始以 grep 来进行正规表示法的简易说明吧!我们先以底下这个档案来作为范例:
[root@test root]# vi regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can’t finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird
|
需要特别注意的是,上面这个档案鸟哥是在 Windows 的环境下编辑的, 并且经过特殊处理过,因此,他虽然是纯文字文件,但是内含一些 Windows 环境下的软件常常自行加入的一些特殊字符,例如断行字符(^M)就是一例! 所以,您可以直接将上面的文字以 vi 储存成 regular_express.txt 这个档案, 不过,比较建议直接点底下的连结下载:
此外,因为不同的语系编码是不一样的,所以,您必须要将语系改成英文语系, 才能够进行底下的测试,否则,可能会有显示的内容与底下的输出不符的状况喔! 修改语系的方法为:
[root@test root]# LANG=en
[root@test root]# export LANG | 好了,现在开始我们一个案例一个案例的来介绍吧!
- 例题一、搜寻特定字符串:
搜寻特定字符串很简单吧?假设我们要从刚刚的档案当中取得 the 这个特定字符串, 最简单的方式就是这样:
[root@test root]# grep -n 'the' regular_express.txt
8:I can't finish the test.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world is the same with "glad".
18:google is the best tools for search keyword.
| 那如果想要『反向选择』呢?也就是说,当该行没有 'the' 这个字符串时,才显示在屏幕上,那就直接使用:
[root@test root]# grep -vn 'the' regular_express.txt | 您会发现,屏幕上出现的行列为除了 8,12,15,16,18 五行之外的其它行列! 接下来,如果您想要取得不论大小写的 the 这个字符串,则:
[root@test root]# grep -in 'the' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
12:the symbol '*' is represented as start.
14:The gd software is a library for drafting programs.
15:You are the best is mean you are the no. 1.
16:The world is the same with "glad".
18:google is the best tools for search keyword.
|
- 例题二、利用 [] 来搜寻集合字符
如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 't?st' 存在~这个时候,我可以这样来搜寻:
[root@test root]# grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good. | 了解了吧?其实 [] 里面不论有几个字符,他都谨代表某『一个』字符, 所以,上面的例子说明了,我需要的字符串是『tast』或『test』两个字符串而已! 而如果想要搜寻到有 oo 的字符时,则使用:
[root@test root]# grep -n 'oo' 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.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes! | 但是,如果我不想要 oo 前面有 g 的话呢?此时,可以利用在集合字符的反向选择 [^] 来达成
[root@test root]# grep -n '[^g]oo' regular_express.txt
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! | 意思就是说,我需要的是 oo ,但是 oo 前面不能是 g 就是了! 仔细比较上面两个表格,妳会发现,第 1,9 行不见了,因为 oo 前面出现了 g 所致! 第 2,3 行没有疑问,因为 foo 与 Foo 均可被接受!但是第 18 行明明有 google 的 goo 啊~ 别忘记了,因为该行后面出现了 tool 的 too 啊!所以该行也被列出来~ 也就是说, 18 行里面虽然出现了我们所不要的项目 (goo) 但是由于有需要的项目 (too) , 因此,是符合字符串搜寻的喔!
至于第 19 行,同样的,因为 goooooogle 里面的 oo 前面可能是 o ,例如: go(ooo)oogle ,所以,这一行也是符合需求的!
再来,假设我 oo 前面不想要有小写字符,所以,我可以这样写 [^abcd....z]oo , 但是这样似乎不怎么方便,由于小写字符的 ASCII 上编码的顺序是连续的, 因此,我们可以将之简化为底下这样:
[root@test root]# grep -n '[^a-z]oo' regular_express.txt
3:Football game is not use feet only. | 也就是说,当我们在一组集合字符中,如果该字符组是连续的,例如大写英文/小写英文/数字等等, 就可以使用[a-z],[A-Z],[0-9]等方式来书写,那么如果我们的要求字符串是数字与英文呢? 呵呵!就将他全部写在一起,变成:[a-zA-Z0-9]
例如,我们要取得有数字的那一行,就这样:
[root@test root]# 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. | 这样对于 [] 以及 [^] 以及 [] 当中的 - 有了解了吗?! ^_^y - 例题三、行首与行尾字符 ^ $:
我们在例题一当中,可以查询到一行字符串里面有 the 的,那如果我想要让 the 只在行首列出呢? 这个时候就得要使用定位字符了!我们可以这样做:
[root@test root]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start. | 此时,就只剩下第 12 行,因为只有第 12 行的行首是 the 开头啊~此外, 如果我想要开头是小写字符的那一行就列出呢?可以这样:
[root@test root]# 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! | 如果我不想要开头是英文字母,则可以是这样:
[root@test root]# grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
20:# I am VBird | 注意到了吧?那个 ^ 符号,在字符集合符号(括号[])之内与之外是不同的! 在 [] 内代表『反向选择』,在 [] 之外则代表定位在行首的意义!要分清楚喔!
那如果我想要找出来,行尾结束为小数点 (.) 的那一行,该如何处理:
[root@test root]# 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 is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
| 特别注意到,因为小数点具有其它意义(底下会介绍),所以必须要使用跳脱字符(\)来加以解除其特殊意义! 不过,您或许会觉得奇怪,但是第 5~9 行最后面也是 . 啊~怎么无法打印出来?? 这里就牵涉到 Windows 平台的软件对于断行字符的判断问题了!我们使用 cat -A 将第五行拿出来看, 您会发现:
[root@test root]# cat -A regular_express.txt
However, this dress is about $ 3183 dollars.^M$ | 注意到了没?最后面的断行字符应该是 $ 才对,但是,因为 Windows 的 nodepad 会主动加上 ^M 作为断行的判断,因此,那个 . 自然就不是紧接在 $ 之前喔!这样可以了解 ^ 与 $ 的意义吗? 好了,先不要看底下的解答,自己想一想,那么如果我想要找出来,哪一行是『空白行』, 也就是说,该行并没有输入任何数据,该如何搜寻??
[root@test root]# grep -n '^$' regular_express.txt
21: | 因为只有行首跟行尾( ^$ ),所以,这样就可以找出空白行啦!再来, 假设您已经知道在一个批次脚本 (shell script) 或者是设定档当中, 空白行与开头为 # 的那一行是批注,因此如果您要将数据列出给别人参考时, 可以将这些数据省略掉,以节省保贵的纸张,那么,您可以怎么作呢? 我们以 /etc/syslog.conf 这个档案来作范例,您可以自行参考一下输出的结果:
[root@test root]# cat /etc/syslog.conf
[root@test root]# grep -v '^$' /etc/syslog.conf | grep -v '^#' | 是否节省很多版面啊?? - 例题四、任意一个字符 . 与重复字符 *
在 bash 的章节当中,我们知道万用字符 * 可以用来代表任意(0或多个)字符, 但是正规表示法并不是万用字符,两者之间是不相同的! 至于正规表示法当中的『 . 』则代表『绝对有一个任意字符』的意思!这样讲不好懂, 我们直接做个练习吧!假设我需要找出 g??d 的字符串,亦即共有四个字符, 起头是 g 而结束是 d ,我可以这样做:
[root@test root]# grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world is the same with "glad".
| 因为强调 g 与 d 之间一定要存在两个字符,因此,第 13 行的 god 与第 14 行的 gd 就不会被列出来啦!再来,如果我想要列出有 oo, ooo, oooo 等等的资料, 也就是说,至少要有两个 o 以上,该如何是好??是 o* 还是 oo* 还是 ooo* 呢? 虽然您可以试看看结果, 不过结果太占版面了 @_@ ,所以,我这里就直接说明。
因为 * 代表的是『重复 0 个或多个前面的 RE 字符』的意义, 因此,『o*』代表的是:『拥有空字符或一个 o 以上的字符』, 特别注意,因为允许空字符(就是有没有字符都可以的意思),因此, grep -n 'o*' regular_express.txt 将会把所有的数据都打印出来屏幕上!
那如果是『oo*』呢?则第一个 o 肯定必须要存在,第二个 o 则是可有可无的多个 o , 所以,凡是含有 o, oo, ooo, oooo 等等,都可以被列出来~
同理,当我们需要『至少两个 o 以上的字符串』时,就需要 ooo* ,亦即是:
[root@test root]# grep -n 'ooo*' 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.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes! | 这样理解 * 的意义了吗?!好了,现在出个练习,如果我想要字符串开头与结尾都是 g, 但是两个 g 之间仅能存在至少一个 o ,亦即是 gog, goog, gooog.... 等等, 那该如何?
[root@test root]# grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes! | 如此了解了吗?好,再来一题,如果我想要找出 g 开头与 g 结尾的字符串, 当中的字符可有可无,那该如何是好?是『g*g』吗?
[root@test root]# grep -n 'g*g' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
9:Oh! The soup taste good.
13:Oh! My god!
14:The gd software is a library for drafting programs.
16:The world is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
19:goooooogle yes!
| 但测试的结果竟然出现这么多行??太诡异了吧? 其实一点也不诡异,因为 g*g 里面的 g* 代表『空字符或一个以上的 g』 在加上后面的 g ,因此,整个 RE 的内容就是 g, gg, ggg, gggg , 因此,只要该行当中拥有一个以上的 g 就符合所需了!
那该如何得到我们的 g....g 的需求呢?呵呵!就利用任意一个字符『.』啊! 亦即是:『g.*g』的作法,因为 * 可以是 0 或多个重复前面的字符,而 . 是任意字符,所以: 『.* 就代表零个或多个任意字符』的意思啦!
[root@test root]# grep -n 'g.*g' regular_express.txt
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! | 因为是代表 g 开头与 g 结尾,中间任意字符均可接受,所以,第 1 与第 14 行是可接受的喔! 这个 .* 的 RE 表示任意字符是很常见的,希望大家能够理解并且熟悉!
再出一题,如果我想要找出『任意数字』的行列呢?因为仅有数字,所以就成为:
[root@test root]# grep -n '[0-9][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. | 虽然使用 grep -n '[0-9]' regular_express.txt 也可以得到相同的结果, 但鸟哥希望大家能够理解上面指令当中 RE 表示法的意义才好! - 例题五、限定连续 RE 字符范围 {}
在上个例题当中,我们可以利用 . 与 RE 字符及 * 来设定 0 个到无线多个重复字符, 那如果我想要限制一个范围区间内的重复字符数呢?举例来说,我想要找出两个到五个 o 的连续字符串,该如何作?这时候就得要使用到限定范围的字符 {} 了。 但因为 { 与 } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用跳脱字符 \ 来让他失去特殊意义才行。
至于 {} 的语法是这样的,假设我要找到两个 o 的字符串,可以是:
[root@test root]# grep -n 'o\{2\}' 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.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes! | 这样看似乎与 ooo* 的字符没有什么差异啊?因为第 19 行有多个 o 依旧也出现了! 好,那么换个搜寻的字符串,假设我们要找出 g 后面接 2 到 5 个 o ,然后再接一个 g 的字符串, 他会是这样:
[root@test root]# grep -n 'go\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword. | 嗯!很好!第 19 行终于没有被取用了(因为 19 行有 6 个 o 啊!)。 那么,如果我想要的是 2 个 o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是:
[root@test root]# grep -n 'go\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes! | 呵呵!就可以找出来啦~
◎grep -- print lines matching a pattern
grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...]
grep用以在file内文中比对相对应的部分,或是当没有指定档案时,由标准输入中去比对。 在预设的情况下,grep会将符合样式的那一行列出。
此外,还有两个程式是grep的变化型,egrep及fgrep其中egrep就等同於grep -E ,fgrep等同於grep -F 。
◎参数
1. -A NUM,--after-context=NUM
除了列出符合行之外,并且列出后NUM行。
ex: $ grep -A 1 panda file
(从file中搜寻有panda样式的行,并显示该行的后1行)
2. -a或--text
grep原本是搜寻文字档,若拿二进位的档案作为搜寻的目标,
则会显示如下的讯息: Binary file 二进位档名 matches 然后结束。
若加上-a参数则可将二进位档案视为文字档案搜寻,
相当於--binary-files=text这个参数。
ex: (从二进位档案mv中去搜寻panda样式)
(错误!!!)
$ grep panda mv
Binary file mv matches
(这表示此档案有match之处,详见--binary-files=TYPE )
$ (正确!!!)
$ grep -a panda mv
3. -B NUM,--before-context=NUM
与 -A NUM 相对,但这此参数是显示除符合行之外
并显示在它之前的NUM行。
ex: (从file中搜寻有panda样式的行,并显示该行的前1行)
$ grep -B 1 panda file
4. -C [NUM], -NUM, --context[=NUM]
列出符合行之外并列出上下各NUM行,预设值是2。
ex: (列出file中除包含panda样式的行外并列出其上下2行)
(若要改变预设值,直接改变NUM即可)
$ grep -C[NUM] panda file
5. -b, --byte-offset
列出样式之前的内文总共有多少byte ..
ex: $ grep -b panda file
显示结果类似於:
0:panda
66:pandahuang
123:panda03
6. --binary-files=TYPE
此参数TYPE预设为binary(二进位),若以普通方式搜寻,只有2种结果:
1.若有符合的地方:显示Binary file 二进位档名 matches
2.若没有符合的地方:什麽都没有显示。
若TYPE为without-match,遇到此参数,
grep会认为此二进位档案没有包含任何搜寻样式,与-I 参数相同。
若TPYE为text, grep会将此二进位档视为text档案,与-a 参数相同。
Warning: --binary-files=text 若输出为终端机,可能会产生一些不必要的输出。
7. -c, --count
不显示符合样式行,只显示符合的总行数。
若再加上-v,--invert-match,参数显示不符合的总行数。
8. -d ACTION, --directories=ACTION
若输入的档案是一个资料夹,使用ACTION去处理这个资料夹。
预设ACTION是read(读取),也就是说此资料夹会被视为一般的档案;
若ACTION是skip(略过),资料夹会被grep略过:
若ACTION是recurse(递),grep会去读取资料夹下所有的档案,
此相当於-r 参数。
9. -E, --extended-regexp
采用规则表示式去解释样式。
10. -e PATTERN, --regexp=PATTERN
把样式做为一个partern,通常用在避免partern用-开始。
11. -f FILE, --file=FILE
事先将要搜寻的样式写入到一个档案,一行一个样式。
然后采用档案搜寻。
空的档案表示没有要搜寻的样式,因此也就不会有任何符合。
ex: (newfile为搜寻样式档)
$grep -f newfile file
12. -G, --basic-regexp
将样式视为基本的规则表示式解释。(此为预设)
13. -H, --with-filename
在每个符合样式行前加上符合的档案名称,若有路径会显示路径。
ex: (在file与testfile中搜寻panda样式)
$grep -H panda file ./testfile
file:panda
./testfile:panda
$
14. -h, --no-filename
与-H参数相类似,但在输出时不显示路径。
15. --help
产生简短的help讯息。
16. -I
grep会强制认为此二进位档案没有包含任何搜寻样式,
与--binary-files=without-match参数相同。
ex: $ grep -I panda mv
17. -i, --ignore-case
忽略大小写,包含要搜寻的样式及被搜寻的档案。
ex: $ grep -i panda mv
18. -L, --files-without-match
不显示平常一般的输出结果,反而显示出没有符合的档案名称。
19. -l, --files-with-matches
不显示平常一般的输出结果,只显示符合的档案名称。
20. --mmap
如果可能,使用mmap系统呼叫去读取输入,而不是预设的read系统呼叫。
在某些状况,--mmap 能产生较好的效能。 然而,--mmap
如果运作中档案缩短,或I/O 错误发生时,
可能造成未定义的行为(包含core dump),。
21. -n, --line-number
在显示行前,标上行号。
ex: $ grep -n panda file
显示结果相似於下:
行号:符合行的内容
22. -q, --quiet, --silent
不显示任何的一般输出。请参阅-s或--no-messages
23. -r, --recursive
递地,读取每个资料夹下的所有档案,此相当於 -d recsuse 参数。
24. -s, --no-messages
不显示关於不存在或无法读取的错误讯息。
小: 不像GNU grep,传统的grep不符合POSIX.2协定,
因为缺乏-q参数,且他的-s 参数表现像GNU grep的 -q 参数。
Shell Script倾向将传统的grep移植,避开-q及-s参数,
且将输出限制到/dev/null。
POSIX: 定义UNIX及UNIX-like系统需要提供的功能。
25. -V, --version
显示出grep的版本号到标准错误。
当您在回报有关grep的bugs时,grep版本号是必须要包含在内的。
26. -v, --invert-match
显示除搜寻样式行之外的全部。
27. -w, --word-regexp
将搜寻样式视为一个字去搜寻,完全符合该"字"的行才会被列出。
28. -x, --line-regexp
将搜寻样式视为一行去搜寻,完全符合该"行"的行才会被列出。
|