grep命令、正則表達式、位置錨定、單詞錨定

grep:根據模式,搜索文本,將匹配的行顯示出來

    語法:grep [OPTIONS] PATTERN [FILE...]

    PATTERN:將文本字符和正則表達式的元字符組合成的匹配條件

            如: grep 'root' /etc/passwd,查找出文件中包含'root'字符的行。

    OPTIONS:

        --color:將匹配到的串用高亮顏色顯示出來 

            如: grep --color 'root' /etc/passwd 

            另:alias grep='grep --color',可定義爲別名   

        -i, --ignore-case

            Ignore case distinctions in both the PATTERN and the input files.

            忽略模式匹配大小寫

如:                

[root@station01 ~]# grep -i 'Root' /etc/passwd
root:$6$yA.3HL1hnKwnfKxO$3Z05q4552ogFjS.LtwgL03d//Svp.Aoud/4GbN8ny/XZEvhv/WxTBtV9mmf5zirQWtblMuufvaxDDz8Tr1ABo1:0:0:root:/root:/bin/bash
operator:*:11:0:operator:/root:/sbin/nologin

        -v:顯示沒有被模式匹配到的行 

        -o:只顯示被模式匹配到的字符串,並且每個串顯示爲一行

       -E:擴展正則表達式

       -A NUM:顯示匹配到行的後NUM行,即After

[root@station01 ~]# grep --color -A 3 'cpu MHz' /proc/cpuinfo   
cpu MHz         : 2128.040
cache size      : 4096 KB
fdiv_bug        : no
hlt_bug         : no

       -B NUM:顯示匹配到行的前NUM行,即Before

[root@station01 ~]# grep --color -B 3 'cpu MHz' /proc/cpuinfo  
model           : 26
model name      : Intel(R) Xeon(R) CPU           E5506  @ 2.13GHz
stepping        : 5
cpu MHz         : 2128.040

       -C NUM:顯示匹配到行的前後NUM行,即Context

[root@station01 ~]# grep --color -C 3 'cpu MHz' /proc/cpuinfo  
model           : 26
model name      : Intel(R) Xeon(R) CPU           E5506  @ 2.13GHz
stepping        : 5
cpu MHz         : 2128.040
cache size      : 4096 KB
fdiv_bug        : no
hlt_bug         : no


如:

[root@station01 ~]# grep -o 'root' /etc/passwd           
root
root
root
root

通配字符:

    

globbing:

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

    ?:任意單個字符

    []:指定範圍內的字符

    [^]:指定範圍外的字符

正則表達式:Regular Expression ,REGEXP,由不表示字符本身意義的元字符組成


元字符:

    .:匹配任意單個字符

如:查找以r開頭t結尾中間是任意兩個字符的字符串

[root@station01 ~]# grep --color 'r..t' /etc/passwd 
root:$6$yA.3HL1hnKwnfKxO$3Z05q4552ogFjS.LtwgL03d//Svp.Aoud/4GbN8ny/XZEvhv/WxTBtV9mmf5zirQWtblMuufvaxDDz8Tr1ABo1:0:0:root:/root:/bin/bash
operator:*:11:0:operator:/root:/sbin/nologin
ftp:*:14:50:FTP User:/var/ftp:/sbin/nologin

    *:表示匹配其前面的字符任意次數,即0到無窮次

如:

    test2.txt文件中的內容如下:

a
b
ab
aab
aaab
acb
addb
amnb


結果:

[root@station01 ~]# grep --color "a*b" ./test2.txt 
b
ab
aab
aaab
acb
addb
amnb


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

如:

結果:

[root@station01 ~]# grep --color "a.*b" ./test2.txt 
ab
aab
aaab
acb
addb
amnb



默認情況下正則表達式工作在貪婪模式下,即尺可能多的匹配


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

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

 [:alnum:]所有字符, [:alpha:]所有字母, [:cntrl:], [:digit:]所有數字, [:graph:], [:lower:]小寫字母, [:print:],

 [:punct:]符號, [:space:]空白字符, [:upper:]大寫字母, [:xdigit:]

如:test2.txt文件

[root@station01 ~]# cat test2.txt 
a
b
ab
aab
aaab
acb
addb
amnb
ambbmnbaaafbaaabadffb

執行結果:

[root@station01 ~]# grep --color "a.*b" ./test2.txt 
ab
aab
aaab
acb
addb
amnb
ambbmnbaaafbaaabadffb


正常應該匹配到第一個amb即可

    \?:匹配其前字符0次或1次,注意模式中?號防需要轉義,即\?

如:

結果:

[root@station01 ~]# grep --color "a\?b" ./test2.txt 
b
ab
aab
aaab
acb
addb
amnb
ambbmnbaaafbaaabadffb


整個串中有部分匹配

    \{m,n\}:匹配其前字符至少m次,至多n次

如:

    \{1,\}:至少1次

    \{,5\}:至多5次

查找a至少出現1次,至多3次,結尾是b的字符串

結果:

[root@station01 ~]# grep --color "a\{1,3\}b" ./test2.txt  
ab
aab
aaab
ambbmnbaaafbaaabadffb

查找a開頭b結尾,中間出現至少1個最多3個的任意字符

[root@station01 ~]# grep --color "a.\{1,3\}b" ./test2.txt  
aab
aaab
acb
addb
amnb
ambbmnbaaafbaaabadffb



位置錨定

    ^:行首錨定,此字符後的任意字符出現在行首

查找/etc/passwd文件中以r開頭,以t結尾,中間出現任意兩個字符的字符串,且出現在行首

結果:

[root@station01 ~]# grep --color '^r..t' /etc/passwd
root:$6$yA.3HL1hnKwnfKxO$3Z05q4552ogFjS.LtwgL03d//Svp.Aoud/4GbN8ny/XZEvhv/WxTBtV9mmf5zirQWtblMuufvaxDDz8Tr1ABo1:0:0:root:/root:/bin/bash

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

查找/etc/inittab中以w結束的行

結果:

[root@station01 ~]# grep --color 'w$' /etc/inittab 
# For information on how to write upstart event handlers, or how


    ^$:空白行

查找/etc/inittab中的空白行

[root@station01 ~]# grep  '^$' /etc/inittab |wc -l
2

查找以數字結尾的行

[root@station01 ~]# grep --color '[[:digit:]]$' /etc/inittab 
#   5 - X11


    單詞錨定

    \<:其後面的字符必須作爲單詞的首部出現,也可\b

    \>:其前的字符必須作爲單詞的尾部出現,也可\b

    \<root\<:root必須作爲一行的單詞出現,也可\broot\b


如文件test3.txt文件內容如下:

[root@station01 ~]# cat test3.txt 
root is administrator.
admin is a rooter.
adminrooter is wrong.
he is heroot

查找root出現在詞首的行

結果:

[root@station01 ~]# grep --color '\<root' test3.txt 
root is administrator.
admin is a rooter.

查找root出現在詞尾的行

結果:

[root@station01 ~]# grep --color 'root\>' test3.txt   
root is administrator.
he is heroot


查找以一個單詞出現的行

結果:

[root@station01 ~]# grep --color '\<root\>' test3.txt 
root is administrator.
[root@station01 ~]# grep --color '\broot\b' test3.txt 
root is administrator.


    分組

    \(\):將一串字符看成一個整體

查找以ab爲一個整體出現的行

文件內容如下:

[root@station01 ~]# cat test2.txt 
a
b
ab
aab
aaab
acb
addb
amnb
ambbmnbaaafbaaabadffb

執行結果:

[root@station01 ~]# grep --color '\(ab\)*' test2.txt 
a
b
ab
aab
aaab
acb
addb
amnb
ambbmnbaaafbaaabadffb

    後向引用:前面出現的字符串,後面引用

    \1:引用第一個左括號和第一個右括號所包括的內容

    \2:同上,只不過是第二個

    \3:同上,只不過是第三個

測試文件內容如下:

[root@station01 ~]# cat test4.txt 
he like his lover
she love her liker
he like his liker
she love her lover

執行結果:

查找以l開頭,e結尾,中間跟兩個任意字符的行

[root@station01 ~]# grep --color 'l..e' test4.txt 
he like his lover
she love her liker
he like his liker
she love her lover

查找以l..e開頭並以l..e結尾中間跟任意個字符的行

[root@station01 ~]# grep --color '\(l..e\).*\1' test4.txt 
he like his liker
she love her lover

查找/etc/inittab中前面是任意數字,並以此數字結尾的行

[root@station01 ~]# grep --color '\([0-9]\).*\1$' /etc/inittab 
#   5 - X11



2015年2月16日於銀川

GB-2312

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