九、grep和正則表達式

一、grep

   grep-print lines matching a pattern(grep 可以分析一行信息,若當中有我們所需要的信息,就將該行拿出來)

   grep [OPTIONS]PATTERN [FILE...](pattern:文本字符和正則表達式的元字符組合而成的匹配條件)

   -A:後面可加數字,爲after的意思,除了列出該行外,後續的n行也列出來

   -B:後面可加數字,爲before的意思,除了列出該行外,前面的n行也列出來

   -C: 後面可加數字,除了列出該行外,前後的n行也列出來

   -E:interpret PATTERN as an extended regular expression(使用擴展正則表達式)

   -a:將binary文本以text文本的方式搜尋數據

   -c:計算找到‘pattern’的次數

   -i:忽略大小寫的不同,所以大小寫視爲相同

   -n:輸出所在行號

   -v:反向選擇,亦即顯示出沒有‘pattern’內容的那一行

   -o:只顯示被匹配到的字符串

   --color=auto(--color):將找到的關鍵詞加上顏色進行顯示

[root@www ~]# grep 'root' -n --color /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

11:operator:x:11:0:operator:/root:/sbin/nologin


二、正則表達式

正則表達式(regular expression,RE)

正則表達式就是用在字符串的處理上面的一項[表示式],正則表達式並不是一個工具程序,而是一個字符串處理的標準依據,如果你想要以正則表達式的方式處理字符串,就得要使用支持正則表達式的工具程序才行,這類的工具程序很多,例如vi,sed,awk,grep等。

正則表達式與通配符是完全不一樣的,因爲通配符(wildcard)代表的是bash操作接口的一個功能,而正則表達式則是一種字符串處理的表達方式。

正則表達式可分爲標準正則表達式和擴展正則表達式

標準正則表達式:

[:space:]:代表會產生空白的字符,包括空格鍵、[tab]、CR等

[:digit:]:代表數字,即0-9

[:lower:]:代表英文小寫字符,即a-z

[:upper:]:代表英文大寫字符,即A-Z

[:alpha:]:代表英文大小寫字符,即A-Z,a-z

[:alnum:]:代表英文大小寫字符及數字,即0-9,A-Z,a-z

[]:匹配指定範圍內的任意單個字符

[^]:匹配指定範圍外的任意單個字符

\<:錨定詞首,此字符後面的任意內容必須出現在詞首,因“<”的符號在shell有特殊意義,所以在“<”前面加 \

\>:錨定詞尾,此字符前面的任意內容必須出現在詞尾,因“>”的符號在shell有特殊意義,所以在“>”前面加 \

^:錨定行首,此字符後面的任意內容必須出現在行首

$:錨定行尾,此字符前面的任意內容必須出現在行尾

^$:空白行

.:匹配任意單個字符

*:重複前一個字符0到無窮多次

.*:任意長度的任意字符

\?:匹配其前面的字符1次或0次,因“?”的符號在shell有特殊意義,所以在“?”前面加反轉字符“\”

\{m,n\}:匹配其前面的字符至少m次,至多n次,因“{}"的符號在shell有特殊意義,所以在“{}”前面加反轉字符“\”

\(\):分組,匹配()內的內容,因“()”的符號在shell有特殊意義,所以在“()”前面加上 \

     \1:引用第一個左括號以及與之對應的右括號所包括的所有內容

(下面都是例子)

[root@www ~]# grep -n 't[ae]st' regular_express.txt 

8:I can't finish the test.

9:Oh! The soup taste good.

[root@www ~]# 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@www ~]# grep -n '^the' regular_express.txt 

12:the symbol '*' is represented as start.

[root@www ~]# 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.

[root@www ~]# grep -n '^$' regular_express.txt 

22:

[root@www ~]# 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 <Happy> is the same with "glad".

[root@www ~]# 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@www ~]# 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!

20:go! go! Let's go.

[root@www ~]# grep -n 'goo\?g' regular_express.txt 

18:google is the best tools for search keyword.

[root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt 

18:google is the best tools for search keyword.


擴展正則表達式:

+ :匹配其前面字符至少一個

{m,n}:匹配其前面的字符至少m次,至多n次,egrep不用在“{}”前加上 \

():分組,匹配()內的內容,egrep不用在“()”前加上 \

    \1:引用第一個左括號以及與之對應的右括號所包括的所有內容

()+:匹配前面的分組至少一個

?:匹配其前面的字符1次或0次,egerp不用再“?”前加上 \

| :或(or)的意思

[root@www ~]# egrep -n 'good|dog' regular_express.txt 

1:"Open Source" is a good mechanism to develop programs.

9:Oh! The soup taste good.

17:I like dog.

其中 . [] [^] *  ^ $ \< \> 所代表的意義跟標準正則表達式一樣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章