正規表示法就是處理字符串的方法,他是以行爲單位來進行字符串的處理行爲,正規表示法透過一些特殊符號的輔助,可讓使用者輕易的達到『搜尋/刪除/取代』某特定字符串的處理程序!git
使用正規表示法時,須要特別留意當時環境的語系爲什麼,不然可能會發現與別人不相同的擷取結果喔!底下的不少練習都是使用『 』這個語系數據來進行express
特殊符號bash |
表明意義app |
[:alnum:]ide |
表明英文大小寫字符及數字,亦即 0-9, A-Z, a-z測試 |
[:alpha:]this |
表明任何英文大小寫字符,亦即 A-Z, a-zgoogle |
[:blank:]編碼 |
表明空格鍵與 [Tab] 按鍵二者spa |
[:cntrl:] |
表明鍵盤上面的控制按鍵,亦即包括 CR, LF, Tab, Del.. 等等 |
[:digit:] |
表明數字而已,亦即 0-9 |
[:graph:] |
除了空格符 (空格鍵與 [Tab] 按鍵) 外的其餘全部按鍵 |
[:lower:] |
表明小寫字符,亦即 a-z |
[:print:] |
表明任何能夠被打印出來的字符 |
[:punct:] |
表明標點符號 (punctuation symbol),亦即:" ' ? ! ; : # $... |
[:upper:] |
表明大寫字符,亦即 A-Z |
[:space:] |
任何會產生空白的字符,包括空格鍵, [Tab], CR 等等 |
[:xdigit:] |
表明 16 進位的數字類型,所以包括:0-9, A-F, a-f 的數字與字符 |
[dmtsai@study ~]$ grep [-A] [-B] [--color=auto] '搜尋字符串' filename
-A :後面可加數字,爲 after 的意思,除了列出該行外,後續的 n 行也列出來;
-B :後面可加數字,爲 befer 的意思,除了列出該行外,前面的 n 行也列出來;
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost tmp]# alias | grep "mv"
[root@localhost tmp]# alias | grep -A1 -B2 "mv"
RE 字符 |
意義與範例 |
^word |
意義:待搜尋的字符串(word)在行首! 範例:搜尋行首爲 # 開始的那一行,並列出行號 grep -n '^#' regular_express.txt |
word$ |
意義:待搜尋的字符串(word)在行尾! 範例:將行尾爲 ! 的那一行打印出來,並列出行號 grep -n '!$' regular_express.txt |
. |
意義:表明『必定有一個任意字符』的字符! 範例:搜尋的字符串能夠是 (eve) (eae) (eee) (e e),但不能僅有 (ee) !亦即 e 與 e 中間『必定』僅有一個字符,而空格符也是字符! grep -n 'e.e' regular_express.txt |
\ |
意義:跳脫字符,將特殊符號的特殊意義去除! 範例:搜尋含有單引號 ' 的那一行! grep -n \' regular_express.txt |
* |
意義:重複零個到無窮多個的前一個 RE 字符 範例:找出含有 (es) (ess) (esss) 等等的字符串,注意,由於 * 能夠是 0 個,因此 es 也是符合帶搜尋字符串。另外,由於 * 爲重複『前一個 RE 字符』的符號,所以,在 * 以前必需要緊接着一個 RE 字符喔!例如任意字符則爲『.*』! grep -n 'ess*' regular_express.txt |
[list] |
意義:字符集合的 RE 字符,裏面列出想要擷取的字符! 範例:搜尋含有 (gl) 或 (gd) 的那一行,須要特別留意的是,在 [] 當中『謹表明一個待搜尋的字符』,例如『 a[afl]y 』表明搜尋的字符串能夠是 aay, afy, aly 即 [afl] 表明 a 或 f 或 l 的意思! grep -n 'g[ld]' regular_express.txt |
[n1-n2] |
意義:字符集合的 RE 字符,裏面列出想要擷取的字符範圍! 範例:搜尋含有任意數字的那一行!需特別留意,在字符集合 [] 中的減號 - 是有特殊意義的,他表明兩個字符之間的全部連續字符!但這個連續與否與 ASCII 編碼有關,所以,你的編碼須要設定正確(在 bash 當中,須要肯定 LANG 與 LANGUAGE 的變量是否正確!) 例如全部大寫字符則爲 [A-Z] grep -n '[A-Z]' regular_express.txt |
[^list] |
意義:字符集合的 RE 字符,裏面列出不要的字符串或範圍! 範例:搜尋的字符串能夠是 (oog) (ood) 但不能是 (oot) ,那個 ^ 在 [] 內時,表明的意義是『反向選擇』的意思。例如,我不要大寫字符,則爲 [^A-Z]。可是,須要特別注意的是,若是以 grep -n [^A-Z] regular_express.txt 來搜尋,卻發現該檔案內的全部行都被列出,爲何?由於這個 [^A-Z] 是『非大寫字符』的意思,由於每一行均有非大寫字符,例如第一行的"Open Source"就有 p,e,n,o.... 等等的小寫字 grep -n 'oo[^t]' regular_express.txt |
\{n,m\} |
意義:連續 n 到 m 個的『前一個 RE 字符』 意義:若爲 \{n\} 則是連續 n 個的前一個 RE 字符, 意義:如果 \{n,\} 則是連續 n 個以上的前一個 RE 字符! 範例:在 g 與 g 之間有 2 個到 3 個的 o 存在的字符串,亦即 (goog)(gooog) grep -n 'go\{2,3\}g' regular_express.txt |
[root@localhost tmp]# cat test
"Open Source" is a good mechanism to develop programs.
Football game is not use feet only.
However, this dress is about $ 3183 dollars.^M
GNU is free air not free beer.^M
the symbol '*' is represented as start.
The gd software is a library for drafting programs.^M
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
google is the best tools for search keyword.
[root@localhost tmp]# grep -n "the" test #查找含有"the"的行
8:I can't finish the test.^M #-n,顯示行號
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -vn "the" test #-v,查找不含"the"的行
[root@localhost tmp]# grep -in "the" test #-i,不區分大小寫
[root@localhost tmp]# grep -n 't[ae]st' test #查找含有test或者tast的行
[root@localhost tmp]# grep -n 'oo' test #查找含有oo的行
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '[^g]oo' test #不是以g開頭的oo
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '[^a-z]oo' test #不是以小寫字母開頭的oo
3:Football game is not use feet only.
[root@localhost tmp]# grep -n '[^[:lower:]]oo' test
[root@localhost tmp]# grep -n '[0-9]' test #含有數字的行
5:However, this dress is about $ 3183 dollars.^M
15:You are the best is mean you are the no. 1.
[root@localhost tmp]# grep -n '[[:digit:]]' test
[root@localhost tmp]# grep -n '^the' test #以the開頭的行
12:the symbol '*' is represented as start.
[root@localhost tmp]# grep -n '^[0-9]' test #以數字開頭的行
[root@localhost tmp]# grep -n '^[[:digit:]]' test
[root@localhost tmp]# grep -n '^[a-z]' test #以小寫字母開頭的行
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '^[[:lower:]]' test
[root@localhost tmp]# grep -n '^[^a-zA-Z]' test #不是以字母開頭的行
1:"Open Source" is a good mechanism to develop programs.
[root@localhost tmp]# grep -n '\.$' test #以.結尾的行
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '^$' test #空行
[root@localhost tmp]# grep -vn '^$' test | grep -vn '^#' #去掉空行和註釋行
u * (星星號):表明『重複前一個字符, 0 到無窮屢次』的意思,爲組合形態
[root@localhost tmp]# grep -n 'g..d' test #g和d中間有兩個字符
1:"Open Source" is a good mechanism to develop programs.
16:The world <Happy> is the same with "glad".
[root@localhost tmp]# grep -n 'ooo*' test #兩個或以上的o
[root@localhost tmp]# grep -n 'goo*g' test #兩個g之間至少有一個o
[root@localhost tmp]# grep -n 'g*g' test #至少一個g
[root@localhost tmp]# grep -n 'g.*g' test #含有兩個g
[root@localhost tmp]# grep -n '[0-9][0-9]*' test #含有數值
[root@localhost tmp]# grep -n '[0-9]' test
[root@localhost tmp]# grep -n 'o\{2\}' test #含有連續兩個o
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n 'ooo*' test
[root@localhost tmp]# grep -n 'o\{2,5\}' test #含有連續2-5個o
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n 'o\{5,\}' test #含有5個或以上個o
[root@localhost tmp]# grep -n 'go\{2,\}g' test #g和g之間含有2個或以上個o
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n 'gooo*g' test
[root@localhost tmp]# grep -n 'go\{2,5\}g' test #g和g之間含有2-5個o
18:google is the best tools for search keyword.