RE字符 意義與範例 ^word 待搜尋的字串(word)在行首。 #範例:grep -n '^#' re.txt 搜尋行首為 # 開始的那一行。 word$ 待搜尋的字串(word)在行尾。 #範例:grep -n '!$' re.txt 將行尾為 ! 的那一行列印出來。 . 代表『任意一個』字符,一定是一個任意字符。 #範例:grep -n 'e.e' re.txt 搜尋的字串可以是 (eve) (eae) (eee) (e e), 但不能僅有 (ee)。 亦即 e 與 e 中間『一定』僅有一個字元,而空白字元也是字元。 \ 跳脫字符,將特殊符號的特殊意義去除。 #範例:grep -n \' re.txt 搜尋含有單引號 ' 的那一行。 * 重複零個或多個的前一個 RE 字符 #範例:grep -n 'ess*' re.txt 找出含有 (es) (ess) (esss) 等等的字串。 \{n,m\} 連續 n 到 m 個的『前一個 RE 字符』 若為 \{n\} 則是連續 n 個的前一個 RE 字符, 若是 \{n,\} 則是連續 n 個以上的前一個 RE 字符。 #範例:grep -n 'go\{2,3\}g' re.txt 在 g 與 g 之間有 2 個到 3 個的 o 存在的字串,亦即 (goog)(gooog) [ ] 在 [ ] 當中『謹代表一個待搜尋的字元』 #範例:grep -n 'g[ld]' re.txt 搜尋含有 (gl) 或 (gd) 的那一行 #範例:grep -n '[0-9]' re.txt 搜尋含有任意數字的那一行。 在字元集合 [ ] 中的減號 - 是代表兩個字元之間的所有連續字元。 [^]:^ 在 [ ] 內時, 代表的意義是『反向選擇』 #範例:grep -n 'oo[^t]' re.txt 搜尋的字串可以是 (oog) (ood) 但不能是 (oot)。
[root@test root]# vi re.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 csie
[root@test root]# grep -n 'the' re.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.
[root@test root]# grep -vn 'the' re.txt # 螢幕上出現的行列為除了 8,12,15,16,18 五行之外的其他行列。
[root@test root]# grep -in 'the' re.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.
[ ]
來搜尋集合字元
[root@test root]# grep -n 't[ae]st' re.txt 8:I can't finish the test. 9:Oh! The soup taste good. # [ ] 裡面不論有幾個字元,都謹代表某『一個』字元。
[root@test root]# grep -n 'oo' re.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!
[^]
[root@test root]# grep -n '[^g]oo' re.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!
[root@test root]# grep -n '[^a-z]oo' re.txt 3:Football game is not use feet only.
[a-zA-Z0-9]
[root@test root]# grep -n '[0-9]' re.txt 5:However, this dress is about $ 3183 dollars. 15:You are the best is mean you are the no. 1.
^ $
:
[root@test root]# grep -n '^the' re.txt 12:the symbol '*' is represented as start. [root@test root]# grep -n '^[a-z]' re.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]' re.txt 1:"Open Source" is a good mechanism to develop programs. 20:# I am csie
[root@test root]# grep -n '\.$' re.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 行最後面也是 .?
^M
作為斷行的判斷。
[root@test root]# cat -A re.txt However, this dress is about $ 3183 dollars.^M$
[root@test root]# grep -n '^$' re.txt 21: [root@test root]# cat /etc/syslog.conf [root@test root]# grep -v '^$' /etc/syslog.conf | grep -v '^#'
#『.』代表『絕對有一個任意字元』 [root@test root]# grep -n 'g..d' re.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". # 『*』代表的是『重複 0 個或多個前面的 RE 字符』 [root@test root]# grep -n 'ooo*' re.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!
# 亦即 gog, goog, gooog.... 等。 [root@test root]# grep -n 'goo*g' re.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
[root@test root]# grep -n 'g*g' re.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, gg, ggg, gggg。
[root@test root]# grep -n 'g.*g' re.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!
[root@test root]# grep -n '[0-9][0-9]*' re.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]' re.txt 也可以得到相同的結果。
{}
:找到兩個 o 的字串。
[root@test root]# grep -n 'o\{2\}' re.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!
[root@test root]# grep -n 'go\{2,5\}g' re.txt 18:google is the best tools for search keyword.
[root@test root]# grep -n 'go\{2,\}g' re.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
[root@test root]# grep -m 1 'go\{2,\}g' re.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
字元或代表意義 | 萬用字元 | 正規表示法的特殊字元 |
* | 0 至無限多個字元 | 重複 0 到多個的前一個 RE 字符 |
反向選擇 | [!range] |
[^range] |
例題:不支援正規表示法的 ls 工具中 『ls -l * 』:代表列出任意檔名的檔案; 『ls -l a* 』:代表列出以 a 為開頭的任何檔名的檔案。 在正規表示法中要找到含有以 a 為開頭的檔案,必須搭配支援正規表示法的工具 grep: ls | grep -n '^a.*'
>
的字行( ! 在正規表示法中並不是特殊字元):
grep -n '[!>]' re.txt