Linux正则表达式学习
以下内容基本参考鸟哥的作品.
grep 查找字符串(以行为单位显示)
grep [-acinv] [--color=auto] '搜寻字符串' filename
参数说明:
-a :将 binary 档案以 text 档案的方式搜寻数据
-c :计算找到 '搜寻字符串' 的次数
-i :忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!
-A : 后面可加数字,将后面多少行也显示出来
-B : 后面可加数字,将前面多少行也显示出来
--color=auto :将查找的字符串加以颜色.
小技巧:可以通过设置别名让grep自动高亮搜索字符. alias grep='grep --color=auto'
以下是鸟哥用来教学的一篇文字,用作练习.
"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
1.搜寻特定字符串
[root@test ~]# grep -n 'the' vbird
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.
2.利用 [] 来搜寻集合字符
[root@test ~]# grep -n 't[ae]st' vbird
8:I can’t finish the test.
9:Oh! The soup taste good.

[root@test ~]# grep -n '[^a-z]oo' vbird
3:Football game is not use feet only.
3.行首与行尾字符 ^ $
[root@test ~]# grep -n '^the' vbird
12:the symbol '*' is represented as start.

[root@test ~]# grep -n '\.$' vbird
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.
20:go! go! Let's go.
4.任意一个字符 . 与重复字符 * [. 代表是一个任意字符 * 代表的是重复 0 个或多个前面的字符]
[root@test ~]# grep -n 'g..d' vbird
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world is the same with "glad".

[root@test ~]# grep -n 'ooo*' vbird
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!

[root@test ~]# grep -n 'goo*g' vbird
18:google is the best tools for search keyword.
19:goooooogle yes!
5.限定连续字符范围 {}
[root@test ~]# grep -n 'o\{2\}' vbird
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!

[root@test ~]# grep -n 'go\{2,5\}g' vbird
18:google is the best tools for search keyword.

[root@test ~]# grep -n 'go\{2,\}g' vbird
18:google is the best tools for search keyword.
19:goooooogle yes!

经过了上面的几个简单的范例,我们可以将基础的正则表达式特殊字符汇整如下:
RE 字符 意义与范例
^word 待搜寻的字符串(word)在行首!
范例:grep -n '^#' regular_express.txt 搜寻行首为 # 开始的那一行!
word$ 待搜寻的字符串(word)在行尾!
范例:grep -n '!$' regular_express.txt 将行尾为 ! 的那一行打印出来!
. 代表『任意一个』字符,一定是一个任意字符!
范例:grep -n 'e.e' regular_express.txt 搜寻的字符串可以是 (eve) (eae) (eee) (e e), 但不能仅有 (ee) !亦即 e 与 e 中间『一定』仅有一个字符,而空格符也是字符!
\ 跳脱字符,将特殊符号的特殊意义去除!
范例:grep -n \' regular_express.txt 搜寻含有单引号 ' 的那一行!
* 重复零个或多个的前一个 RE 字符
范例:grep -n 'ess*' regular_express.txt 找出含有 (es) (ess) (esss) 等等的字符串,注意,因为 * 可以是 0 个,所以 es 也是符合带搜寻字符串。另外,因为 * 为重复『前一个 RE 字符』的符号, 因此,在 * 之前必须要紧接着一个 RE 字符喔!例如任意字符则为 『.*』 !
\{n,m\} 连续 n 到 m 个的『前一个 RE 字符』 若为 \{n\} 则是连续 n 个的前一个 RE 字符, 若是 \{n,\} 则是连续 n 个以上的前一个 RE 字符!
范例:grep -n 'go\{2,3\}g' regular_express.txt 在 g 与 g 之间有 2 个到 3 个的 o 存在的字符串,亦即 (goog)(gooog)
[] 字符集合的 RE 特殊字符的符号
[list] 范例:grep -n 'g[ld]' regular_express.txt 搜寻含有 (gl) 或 (gd) 的那一行~ 需要特别留意的是,在 [] 当中『谨代表一个待搜寻的字符』, 例如: a[afl]y 代表搜寻的字符串可以是 aay, afy, aly 亦即 [afl] 代表 a 或 f 或 l 的意思! [ch1-ch2] 范例:grep -n '[0-9]' regular_express.txt 搜寻含有任意数字的那一行!需特别留意,在字符集合 [] 中的减号 - 是有特殊意义的,他代表两个字符之间的所有连续字符!但这个连续与否与 ASCII 编码有关, 因此,您的编码需要设定正确(在 bash 当中,需要确定 LANG 与 LANGUAGE 的变量是否正确!) 例如所有大写字符则为 [A-Z] [^] 范例:grep -n 'oo[^t]' regular_express.txt 搜寻的字符串可以是 (oog) (ood) 但不能是 (oot) ,那个 ^ 在 [] 内时, 代表的意义是『反向选择』的意思~例如,我不要大写字符,则为 [^A-Z] ~ 但是,需要特别注意的是,如果以 grep -n [^A-Z] regular_express.txt 来搜寻, 却发现该档案内的所有行都被列出,为什么?因为这个 [^A-Z] 是『非大写字符』的意思, 因为每一行均有非大写字符,例如第一行的 "Open Source" 就有 p,e,n,o.... 等等的小写字符, 以及双引号 (") 等字符,所以当然符合 [^A-Z] 的搜寻!