轉自:http://blog.csdn.net/hellochenlian/article/details/34088179正則表達式
grep用法詳解:grep與正則表達式 首先要記住的是: 正則表達式與通配符不同,它們表示的含義並不相同! 正則表達式只是一種表示法,只要工具支持這種表示法,那麼該工具就能夠處理正則表達式的字符串。vi grep ,awk ,sed 等都支持正則表達式. 1基礎正則表達式 grep 工具,之前介紹過。 grep -[acinv] '搜索內容串' filename -a 以文本文件方式搜索 -c 計算找到的符合行的次數 -i 忽略大小寫 -n 順便輸出行號 -v 反向選擇,即找 沒有搜索字符串的行 其中搜索串能夠是正則表達式! 1 搜索有the的行,並輸出行號 $grep -n 'the' regular_express.txt 搜索沒有the的行,並輸出行號 $grep -nv 'the' regular_express.txt 2 利用[]搜索集合字符 [] 表示其中的某一個字符 ,例如[ade] 表示a或d或e woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_express.txt 8:I can't finish the test. 9:Oh! the soup taste good! 能夠用^符號作[]內的前綴,表示除[]內的字符以外的字符。 好比搜索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. ..... 注意在MS的系統下生成的文本文件,換行會加上一個 ^M 字符。因此最後的字符會是隱藏的^M ,在處理Windows 下面的文本時要特別注意! 能夠用cat dos_file | tr -d '\r' > unix_file 來刪除^M符號。 ^M==\r 那麼'^$' 就表示只有行首行尾的空行拉! 搜索空行 woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt 22: 23: woody@xiaoc:~/tmp$ 搜索非空行 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. .......... 任意一個字符. 與重複字符 * 在bash中*表明通配符,用來表明任意個字符,可是在正則表達式中,他含義不一樣,*表示有0個或多個 某個字符。 例如 oo*, 表示第一個o必定存在,第二個o能夠有一個或多個,也能夠沒有,所以表明至少一個o. 點. 表明一個任意字符,必須存在。 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. 限定連續重複字符的範圍 { } . * 只能限制0個或多個, 若是要確切的限制字符重複數量,就用{範圍} 。範圍是數字用,隔開 2,5 表示2~5個, 2表示2個,2, 表示2到更多個 注意,因爲{ }在SHELL中有特殊意義,所以做爲正則表達式用的時候要用\轉義一下。 搜索包含兩個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! 注意,相讓[]中的^ - 不表現特殊意義,能夠放在[]裏面內容的後面。 '[^a-z\.!^ -]' 表示沒有小寫字母,沒有. 沒有!, 沒有空格,沒有- 的 串,注意[]裏面有個小空格。 另外shell 裏面的反向選擇爲[!range], 正則裏面是 [^range] 2擴展正則表達式 擴展正則表達式是對基礎正則表達式添加了幾個特殊構成的。 它令某些操做更加方便。 好比咱們要去除 空白行和行首爲 #的行, 會這樣用: 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. .................... 這裏| 表示或的關係。 即知足 ^$ 或者 ^# 的字符串。 這裏列出幾個擴展特殊符號: +,於 . * 做用相似,表示 一個或多個重複字符。 ?, 於 . * 做用相似,表示0個或一個字符。 |,表示或關係,好比 'gd|good|dog' 表示有gd,good或dog的串 (),將部份內容合成一個單元組。好比 要搜索 glad 或 good 能夠這樣 'g(la|oo)d' ()的好處是能夠對小組使用 + ? * 等。 好比要搜索A和C開頭結尾,中間有至少一個(xyz) 的串,能夠這樣 : 'A(xyz)+C' ◎grep -- print lines matching a pattern (將符合樣式的該行列出) ◎語法: grep [options] PATTERN [FILE...] grep用以在file內文中比對相對應的部分,或是當沒有指定檔案時, 由標準輸入中去比對。在預設的狀況下,grep會將符合樣式的那一行列出。 此外,還有兩個程式是grep的變化型,egrep及fgrep。 其中egrep就等同於grep -E ,fgrep等同於grep -F 。 ◎參數 1. -A NUM,--after-context=NUM 除了列出符合行以外,而且列出後NUM行。 ex: $ grep -A 1 panda file (從file中搜尋有panda樣式的行,並顯示該行的後1行) 2. -a或--text grep本來是搜尋文字檔,若拿二進位的檔案做爲搜尋的目標, 則會顯示以下的訊息: Binary file 二進位檔名 matches 然後結束。 若加上-a參數則可將二進位檔案視爲文字檔案搜尋, 至關於--binary-files=text這個參數。 ex: (從二進位檔案mv中去搜尋panda樣式) (錯誤!!!) $ grep panda mv Binary file mv matches (這表示此檔案有match之處,詳見--binary-files=TYPE ) $ (正確!!!) $ grep -a panda mv 3. -B NUM,--before-context=NUM 與 -A NUM 相對,但這此參數是顯示除符合行以外 並顯示在它以前的NUM行。 ex: (從file中搜尋有panda樣式的行,並顯示該行的前1行) $ grep -B 1 panda file 4. -C [NUM], -NUM, --context[=NUM] 列出符合行以外並列出上下各NUM行,預設值是2。 ex: (列出file中除包含panda樣式的行外並列出其上下2行) (若要改變預設值,直接改變NUM便可) $ grep -C[NUM] panda file 5. -b, --byte-offset 列出樣式以前的內文總共有多少byte .. ex: $ grep -b panda file 顯示結果相似於: 0:panda 66:pandahuang 123:panda03 6. --binary-files=TYPE 此參數TYPE預設爲binary(二進位),若以普通方式搜尋,只有2種結果: 1.如有符合的地方:顯示Binary file 二進位檔名 matches 2.若沒有符合的地方:什麼都沒有顯示。 若TYPE爲without-match,遇到此參數, grep會認爲此二進位檔案沒有包含任何搜尋樣式,與-I 參數相同。 若TPYE爲text, grep會將此二進位檔視爲text檔案,與-a 參數相同。 Warning: --binary-files=text 若輸出爲終端機,可能會產生一些沒必要要的輸出。 7. -c, --count 不顯示符合樣式行,只顯示符合的總行數。 若再加上-v,--invert-match,參數顯示不符合的總行數。 8. -d ACTION, --directories=ACTION 若輸入的檔案是一個資料夾,使用ACTION去處理這個資料夾。 預設ACTION是read(讀取),也就是說此資料夾會被視爲通常的檔案; 若ACTION是skip(略過),資料夾會被grep略過: 若ACTION是recurse(遞),grep會去讀取資料夾下全部的檔案, 此至關於-r 參數。 9. -E, --extended-regexp 採用規則表示式去解釋樣式。 10. -e PATTERN, --regexp=PATTERN 把樣式作爲一個partern,一般用在避免partern用-開始。 11. -f FILE, --file=FILE 事先將要搜尋的樣式寫入到一個檔案,一行一個樣式。 然後採用檔案搜尋。 空的檔案表示沒有要搜尋的樣式,所以也就不會有任何符合。 ex: (newfile爲搜尋樣式檔) $grep -f newfile file 12. -G, --basic-regexp 將樣式視爲基本的規則表示式解釋。(此爲預設) 13. -H, --with-filename 在每一個符合樣式行前加上符合的檔案名稱,如有路徑會顯示路徑。 ex: (在file與testfile中搜尋panda樣式) $grep -H panda file ./testfile file:panda ./testfile:panda $ 14. -h, --no-filename 與-H參數相相似,但在輸出時不顯示路徑。 15. --help 產生簡短的help訊息。 16. -I grep會強制認爲此二進位檔案沒有包含任何搜尋樣式, 與--binary-files=without-match參數相同。 ex: $ grep -I panda mv 17. -i, --ignore-case 忽略大小寫,包含要搜尋的樣式及被搜尋的檔案。 ex: $ grep -i panda mv 18. -L, --files-without-match 不顯示日常通常的輸出結果,反而顯示出沒有符合的檔案名稱。 19. -l, --files-with-matches 不顯示日常通常的輸出結果,只顯示符合的檔案名稱。 20. --mmap 若是可能,使用mmap系統呼叫去讀取輸入,而不是預設的read系統呼叫。 在某些情況,--mmap 能產生較好的效能。然而,--mmap 若是運做中檔案縮短,或I/O 錯誤發生時, 可能形成未定義的行爲(包含core dump),。 21. -n, --line-number 在顯示行前,標上行號。 ex: $ grep -n panda file 顯示結果類似於下: 行號:符合行的內容 22. -q, --quiet, --silent 不顯示任何的通常輸出。請參閱-s或--no-messages 23. -r, --recursive 遞地,讀取每一個資料夾下的全部檔案,此至關於 -d recsuse 參數。 24. -s, --no-messages 不顯示關於不存在或沒法讀取的錯誤訊息。 小: 不像GNU grep,傳統的grep不符合POSIX.2協定, 由於缺少-q參數,且他的-s 參數表現像GNU grep的 -q 參數。 Shell Script傾向將傳統的grep移植,避開-q及-s參數, 且將輸出限制到/dev/null。 POSIX: 定義UNIX及UNIX-like系統須要提供的功能。 25. -V, --version 顯示出grep的版本號到標準錯誤。 當您在回報有關grep的bugs時,grep版本號是必需要包含在內的。 26. -v, --invert-match 顯示除搜尋樣式行以外的所有。 27. -w, --word-regexp 將搜尋樣式視爲一個字去搜尋,徹底符合該"字"的行纔會被列出。 28. -x, --line-regexp grep參數 1. -c 顯示匹配的行數(就是顯示有多少行匹配了); 2. -n 顯示匹配內容所在文檔的行號; 3. -i 匹配時忽略大小寫; 4. -s 錯誤信息不輸出; 5. -v 輸出不匹配內容; 6. -x 輸出徹底匹配內容; 7. \ 忽略表達式中字符原有含義; 8. ^ 匹配表達式的開始行; 9. $ 匹配表達式的結束行; 10. \< 從匹配表達式的行開始; 11. \> 到匹配表達式的行結束; 12. [ ] 單個字符(如[A] 即A符合要求); 13. [ - ] 範圍;如[A-Z]即A,B,C一直到Z都符合要求; 14. . 全部的單個字符; 15. * 全部字符,長度能夠爲0; [精華] Grep 用法 Grep : g (globally) search for a re (regular expression ) and p (print ) the results. 1、參數: -I :忽略大小寫 -c :打印匹配的行數 -l :從多個文件中查找包含匹配項 -v :查找不包含匹配項的行 -n:打印包含匹配項的行和行標 2、RE(正則表達式) \ 忽略正則表達式中特殊字符的原有含義 ^ 匹配正則表達式的開始行 $ 匹配正則表達式的結束行 \< 從匹配正則表達式的行開始 \>; 到匹配正則表達式的行結束 [ ] 單個字符;如[A] 即A符合要求 [ - ] 範圍 ;如[A-Z]即A,B,C一直到Z都符合要求 . 全部的單個字符 * 全部字符,長度能夠爲0 3、舉例 # ps -ef | grep in.telnetd root 19955 181 0 13:43:53 ? 0:00 in.telnetd # more size.txt size文件的內容 b124230 b034325 a081016 m7187998 m7282064 a022021 a061048 m9324822 b103303 a013386 b044525 m8987131 B081016 M45678 B103303 BADc2345 # more size.txt | grep '[a-b]' 範圍 ;如[A-Z]即A,B,C一直到Z都符合要求 b124230 b034325 a081016 a022021 a061048 b103303 a013386 b044525 # more size.txt | grep '[a-b]'* b124230 b034325 a081016 m7187998 m7282064 a022021 a061048 m9324822 b103303 a013386 b044525 m8987131 B081016 M45678 B103303 BADc2345 # more size.txt | grep '' 單個字符;如[A] 即A符合要求 b124230 b034325 b103303 b044525 # more size.txt | grep '[bB]' b124230 b034325 b103303 b044525 B081016 B103303 BADc2345 # grep 'root' /etc/group root::0:root bin::2:root,bin,daemon sys::3:root,bin,sys,adm adm::4:root,adm,daemon uucp::5:root,uucp mail::6:root tty::7:root,tty,adm lp::8:root,lp,adm nuucp::9:root,nuucp daemon::12:root,daemon # grep '^root' /etc/group 匹配正則表達式的開始行 root::0:root # grep 'uucp' /etc/group uucp::5:root,uucp nuucp::9:root,nuucp # grep '\<uucp' /etc/group uucp::5:root,uucp # grep 'root$' /etc/group 匹配正則表達式的結束行 root::0:root mail::6:root # more size.txt | grep -i 'b1..*3' -i :忽略大小寫 b124230 b103303 B103303 # more size.txt | grep -iv 'b1..*3' -v :查找不包含匹配項的行 b034325 a081016 m7187998 m7282064 a022021 a061048 m9324822 a013386 b044525 m8987131 B081016 M45678 BADc2345 # more size.txt | grep -in 'b1..*3' 1:b124230 9:b103303 15:B103303 # grep '$' /etc/init.d/nfs.server | wc -l 128 # grep '\$' /etc/init.d/nfs.server | wc –l 忽略正則表達式中特殊字符的原有含義 15 # grep '\$' /etc/init.d/nfs.server case "$1" in >;/tmp/sharetab. [ "x$fstype" != xnfs ] && \ echo "$path\t$res\t$fstype\t$opts\t$desc" \ >;>;/tmp/sharetab. /usr/bin/touch -r /etc/dfs/sharetab /tmp/sharetab. /usr/bin/mv−f/tmp/sharetab. /etc/dfs/sharetab if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v '^[ ]*(#|$)' \ if [ $startnfsd -eq 0 -a -f /etc/rmmount.conf ] && \ if [ $startnfsd -ne 0 ]; then elif [ ! -n "$_INIT_RUN_LEVEL" ]; then while [ $wtime -gt 0 ]; do wtime=`expr $wtime - 1` if [ $wtime -eq 0 ]; then echo "Usage: $0 { start | stop }" # more size.txt the test file their are files The end # grep 'the' size.txt the test file their are files # grep '\<the' size.txt the test file their are files # grep 'the\>;' size.txt the test file # grep '\<the\>;' size.txt the test file # grep '\<[Tt]he\>;' size.txt the test file The end 何爲轉義:將特殊符號當普通符號來處理 筆記: 1.^在[]內外的含義 2.什麼時候須要轉義 3.*在bash中和正則表達式中自己的區別 4.-acinv 2014年6月24日11:24:50