[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 这个档案, 不过,比较建议直接点底下的连结下载:
[url]http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt[/url]此外,因为不同的语系编码是不一样的,所以,您必须要将语系改成英文语系, 才能够进行底下的测试,否则,可能会有显示的内容与底下的输出不符的状况喔! 修改语系的方法为:
[root@test root]#
[root@test root]#
好了,现在开始我们一个案例一个案例的来介绍吧!
例题一、搜寻特定字符串: 搜寻特定字符串很简单吧?假设我们要从刚刚的档案当中取得 the 这个特定字符串, 最简单的方式就是这样:
[root@test root]#
8:I can't finish test.
12: symbol '*' is represented as start.
15:You are best is mean you are the no. 1.
16:The world is same with "glad".
18:google is best tools for search keyword.
您会发现,屏幕上出现的行列为除了 8,12,15,16,18 五行之外的其它行列! 接下来,如果您想要取得不论大小写的 the 这个字符串,则:
[root@test root]#
8:I can't finish test.
9:Oh! soup taste good.
12: symbol '*' is represented as start.
14: gd software is a library for drafting programs.
15:You are best is mean you are the no. 1.
16: world is same with "glad".
18:google is best tools for search keyword.
例题二、利用 [] 来搜寻集合字符 如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 't?st' 存在~这个时候,我可以这样来搜寻:
[root@test root]#
8:I can't finish the .
9:Oh! The soup e good.
[root@test root]#
1:"Open Source" is a gd mechanism to develop programs.
2:apple is my favorite fd.
3:Ftball game is not use feet only.
9:Oh! The soup taste gd.
18:ggle is the best tools for search keyword.
19:goooogle yes!
但是,如果我不想要 oo 前面有 g 的话呢?此时,可以利用在集合字符的反向选择 [^] 来达成
[root@test root]#
2:apple is my favorite d.
3:tball game is not use feet only.
18:google is the best ls for search keyword.
19:gooogle yes!
意思就是说,我需要的是 oo ,但是 oo 前面不能是 g 就是了! 仔细比较上面两个表格,妳会发现,第 1,9 行不见了,因为 oo 前面出现了 g 所致! 第 2,3 行没有疑问,因为 foo 与 Foo 均可被接受!但是第 18 行明明有 google 的 goo 啊~ 别忘记了,因为该行后面出现了 tool 的 too 啊!所以该行也被列出来~ 也就是说, 18 行里面虽然出现了我们所不要的项目 (goo) 但是由于有需要的项目 (too) , 因此,是符合字符串搜寻的喔!
[root@test root]#
5:However, this dress is about $ dollars.
15:You are the best is mean you are the no. .
这样对于 [] 以及 [^] 以及 [] 当中的 - 有了解了吗?! ^_^y
例题三、行首与行尾字符 ^ $: 我们在例题一当中,可以查询到一行字符串里面有 the 的,那如果我想要让 the 只在行首列出呢? 这个时候就得要使用定位字符了!我们可以这样做:
[root@test root]#
12: symbol '*' is represented as start.
此时,就只剩下第 12 行,因为只有第 12 行的行首是 the 开头啊~此外, 如果我想要开头是小写字符的那一行就列出呢?可以这样:
[root@test root]#
2:pple is my favorite food.
4:his dress doesn't fit me.
10:otorcycle is cheap than car.
12:he symbol '*' is represented as start.
18:oogle is the best tools for search keyword.
19:oooooogle yes!
如果我不想要开头是英文字母,则可以是这样:
[root@test root]#
1:Open Source" is a good mechanism to develop programs.
20: I am VBird
[root@test root]#
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]#
However, this dress is about $ 3183 dollars.$
[root@test root]#
1:"Open Source" is a mechanism to develop programs.
9:Oh! The soup taste .
16:The world is the same with "".
因为强调 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]#
1:"Open Source" is a gd mechanism to develop programs.
2:apple is my favorite fd.
3:Ftball game is not use feet only.
9:Oh! The soup taste gd.
18:ggle is the best tls for search keyword.
19:ggle yes!
这样理解 * 的意义了吗?!好了,现在出个练习,如果我想要字符串开头与结尾都是 g, 但是两个 g 之间仅能存在至少一个 o ,亦即是 gog, goog, gooog.... 等等, 那该如何?
[root@test root]#
18:le is the best tools for search keyword.
19:le yes!
如此了解了吗?好,再来一题,如果我想要找出 g 开头与 g 结尾的字符串, 当中的字符可有可无,那该如何是好?是『g*g』吗?
[root@test root]#
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 就符合所需了!
[root@test root]#
1:"Open Source" is a rams.
14:The rams.
18:le is the best tools for search keyword.
19:le yes!
因为是代表 g 开头与 g 结尾,中间任意字符均可接受,所以,第 1 与第 14 行是可接受的喔! 这个 .* 的 RE 表示任意字符是很常见的,希望大家能够理解并且熟悉!
再出一题,如果我想要找出『任意数字』的行列呢?因为仅有数字,所以就成为:
[root@test root]#
5:However, this dress is about $ dollars.
15:You are the best is mean you are the no. .
虽然使用 grep -n '[0-9]' regular_express.txt 也可以得到相同的结果, 但鸟哥希望大家能够理解上面指令当中 RE 表示法的意义才好!
例题五、限定连续 RE 字符范围 {} 在上个例题当中,我们可以利用 . 与 RE 字符及 * 来设定 0 个到无线多个重复字符, 那如果我想要限制一个范围区间内的重复字符数呢?举例来说,我想要找出两个到五个 o 的连续字符串,该如何作?这时候就得要使用到限定范围的字符 {} 了。 但因为 { 与 } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用跳脱字符 \ 来让他失去特殊意义才行。
至于 {} 的语法是这样的,假设我要找到两个 o 的字符串,可以是:
[root@test root]#
1:"Open Source" is a gd mechanism to develop programs.
2:apple is my favorite fd.
3:Ftball game is not use feet only.
9:Oh! The soup taste gd.
18:ggle is the best tls for search keyword.
19:goooogle yes!
这样看似乎与 ooo* 的字符没有什么差异啊?因为第 19 行有多个 o 依旧也出现了! 好,那么换个搜寻的字符串,假设我们要找出 g 后面接 2 到 5 个 o ,然后再接一个 g 的字符串, 他会是这样:
[root@test root]#
18:le is the best tools for search keyword.
嗯!很好!第 19 行终于没有被取用了(因为 19 行有 6 个 o 啊!)。 那么,如果我想要的是 2 个 o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是:
[root@test root]#
18:le is the best tools for search keyword.
19:le yes!
我们先来谈一谈 sed 好了,基本上, sed 可以分析 Standard Input (STDIN) 的数据, 然后将数据经过处理后,再将他输出到 standrad out (STDOUT) 的一个工具。 至于处理呢?可以进行取代、删除、新增、撷取特定行等等的功能呢!很不错吧~ 我们先来了解一下 sed 的用法,再来聊他的用途好了!
总之,这个 sed 不错用啦!而且很多的 shell script 都会使用到这个指令的功能~ sed 可以帮助系统管理员管理好日常的工作喔!要仔细的学习呢!
awk 工具简介
相较于 sed 常常作用于一整个行的处理, awk 则比较倾向于一行当中分成数个『字段』来处理。 因此,awk 相当的适合处理小型的数据数据处理呢!awk 通常运作的模式是这样的:
[root@linux ~]#
awk 可以处理后续接的档案,也可以读取来自前个指令的 standard output 。 但如前面说的, awk 主要是处理『每一行的字段内的数据』,而预设的『字段的分隔符为 "空格键" 或 "[tab]键" 』!举例来说,我们用 last 可以将登入者的数据取出来, 结果如下所示:
[root@linux ~]#
dmtsai pts/0 192.168.1.12 Mon Aug 22 09:40 still logged in
root tty1 Mon Aug 15 11:38 - 11:39 (00:01)
reboot system boot 2.6.11 Sun Aug 14 18:18 (7+15:41)
dmtsai pts/0 192.168.1.12 Fri Aug 12 12:07 - 12:08 (00:01)