可以用^符號做[]內的前綴,表示除[]內的字符之外的字 符。 比如搜索oo前沒有g的字符串所在的行. 使用 '[^g]oo' 作搜索字符串 woody@xiaoc:~/tmp$ 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!
[] 內可以用范圍表示,比如[a-z] 表示小寫字母,[0-9] 表示0~9的數字, [A-Z] 則是大寫字母們。[a-zA-Z0-9]表示所有數字與英文字符。 當然也可以配合^來排除字符。 搜索包含數字的行 woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt 5:However ,this dress is about $ 3183 dollars. 15:You are the best is menu you are the no.1.
行首與行尾字符 ^ $. ^ 表示行的開頭,$表示行的結尾( 不是字符,是位置)那么‘^$' 就表示空行,因為只有 行首和行尾。 這里^與[]里面使用的^意義不同。它表示^后面的串是在行的開頭。 比如搜索the在開頭的行 woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt 12:the symbol '*' is represented as star.
搜索以小寫字母開頭的行 woody@xiaoc:~/tmp$ 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 star. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go. woody@xiaoc:~/tmp$
搜索開頭不是英文字母的行 woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 21:#I am VBird woody@xiaoc:~/tmp$
$表示它前面的串是在行的結尾,比如 '\.' 表示 . 在一行的結尾 搜索末尾是.的行 woody@xiaoc:~/tmp$ 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. 5:However ,this dress is about $ 3183 dollars. 6:GNU is free air not free beer. .....
搜索非空行 woody@xiaoc:~/tmp$ grep -vn '^$' 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. ..........
點. 代表一個任意字符,必須存在。 g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。
woody@xiaoc:~/tmp$ 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 is the same with 'glad'. woody@xiaoc:~/tmp$
搜索兩個o以上的字符串 woody@xiaoc:~/tmp$ grep -n 'ooo*' regular_express.txt //前兩個o一定存在,第三個o可沒有,也可有多個。 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!
搜索g開頭和結尾,中間是至少一個o的字符串,即gog, goog....gooog...等 woody@xiaoc:~/tmp$ grep -n 'goo*g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
搜索g開頭和結尾的字符串在的行 woody@xiaoc:~/tmp$ grep -n 'g.*g' regular_express.txt // .*表示 0個或多個任意字符 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! 20:go! go! Let's go.
搜索包含兩個o的字符串的行。 woody@xiaoc:~/tmp$ 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!
搜索g后面跟2~5個o,后面再跟一個g的字符串的行。 woody@xiaoc:~/tmp$ grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword.
搜索包含g后面跟2個以上o,后面再跟g的行。。 woody@xiaoc:~/tmp$ grep -n 'go\{2,\}g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
擴展正則表達式是對基礎正則表達式添加了幾個特殊構成的。 它令某些操作更加方便。 比如我們要去除 空白行和行首為 #的行, 會這樣用: woody@xiaoc:~/tmp$ grep -v '^$' regular_express.txt | grep -v '^#' "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. ............
然而使用支持擴展正則表達式的 egrep 與擴展特殊符號 | ,會方便許多。 注意grep只支持基礎表達式, 而egrep 支持擴展的, 其實 egrep 是 grep -E 的別名而已。因此grep -E 支持擴展正則。 那么: woody@xiaoc:~/tmp$ egrep -v '^$|^#' 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. .................... 這里| 表示或的關系。 即滿足 ^$ 或者 ^# 的字符串。