轉自鳥哥的linux私房菜 node
前言
linux
Tips: 有一點要向你們報告的,那就是:『正則表達式與萬用字符是不同的東西!』 這很重要喔!由於萬用字符 (wildcard) 所表明的意義與正則表達式並不相同~ 要分的很清楚才行喔!因此,學習本章,請將前一章 bash 的萬用字符意義先忘掉吧! |
![]() |
[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 纔將數據秀出來到屏幕上!
|
[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 I like dog. google is the best tools for search keyword. goooooogle yes! go! go! Let's go. # I am VBird |
[root@test root]# LANG=en [root@test root]# export LANG
|
[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 |
[root@test root]# grep -vn 'the' regular_express.txt
|
[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 |
[root@test root]# grep -n 't[ae]st' regular_express.txt 8:I can't finish the test. 9:Oh! The soup taste good.
|
[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!
|
[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!
|
[root@test root]# grep -n '[^a-z]oo' regular_express.txt 3:Football game is not use feet only.
|
[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.
|
[root@test root]# grep -n '^the' regular_express.txt 12:the symbol '*' is represented as start.
|
[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 |
[root@test root]# cat -A regular_express.txt However, this dress is about $ 3183 dollars.^M$
|
[root@test root]# grep -n '^$' regular_express.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' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 9:Oh! The soup taste good. 16:The world |
[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!
|
[root@test root]# grep -n 'goo*g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
|
[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 |
[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!
|
[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.
|
[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!
|
[root@test root]# grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword.
|
[root@test root]# grep -n 'go\{2,\}g' regular_express.txt 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] 的搜尋! |
RE 字符 | 意義與範例 |
+ | 重複『一個或一個以上』的前一個 RE 字符 |
範例:egrep -n 'go+d' regular_express.txt 搜尋 (god) (good) (goood)... 等等的字符串。 那個 o+ 表明『一個以上的 o 』因此,上面的執行成果會將第 1, 9, 13 行列出來。 |
|
? | 『零個或一個』的前一個 RE 字符 |
範例:egrep -n 'go?d' regular_express.txt 搜尋 (gd) (god) 這兩個字符串。 那個 o? 表明『空的或 1 個 o 』因此,上面的執行成果會將第 13, 14 行列出來。 有沒有發現到,這兩個案例( 'go+d' 與 'go?d' )的結果集合與 'go*d' 相同? 想一想看,這是爲何喔! ^_^ |
|
| | 用或( or )的方式找出數個字符串 |
範例:egrep -n 'gd|good' regular_express.txt 搜尋 gd 或 good 這兩個字符串,注意,是『或』! 因此,第 1,9,14 這三行均可以被打印出來喔!那若是還想要找出 dog 呢?就這樣啊: egrep -n 'gd|good|dog' regular_express.txt |
|
( ) | 找出『羣組』字符串 |
範例:egrep -n 'g(la|oo)d' regular_express.txt 搜尋 (glad) 或 (good) 這兩個字符串,由於 g 與 d 是重複的,因此, 我就能夠將 la 與 oo 列於 ( ) 當中,並以 | 來分隔開來,就能夠啦! 此外,這個功能還能夠用來做爲『多個重複羣組』的判別喔!舉例來講: echo 'AxyzxyzxyzxyzC' | egrep 'A(xyz)+C' 上面的例子當中,意思是說,我要找開頭是 A 結尾是 C ,中間有一個以上的 "xyz" 字符串的意思~ |