天天看點

grep、egrep和正規表達式的總結

本文主要講述grep和egrep的使用,已經在這兩個指令中用到的正規表達式的一些簡單的應用和事例

一、grep的使用

1.grep的解釋

   grep為global search regular expression(RE) and print out the line的縮寫,即根據使用者指定的文本搜尋模式對目标檔案進行搜尋并顯示能夠被模式比對到的行的一種文本搜尋工具。

2.grep的使用格式

   gerp [options] 'PATTERN'file,....

  其中PATTERN項需要使用''或者"",如果需要對模式進行轉換,則需要使用"",如果不需要進行轉換,則使用''或""都可以。模式還可以使用正規表達式來表示。

3.grep的常用選項

   --color:用來指定被模式比對到的字元的顯示顔色,參數選項有never,always和auto

   -v:反向比對,即不能被模式所比對到的行,也可以使用--invert-match長選項

   -o:隻顯示被模式比對到字元串,而非顯示整行(grep預設顯示被比對到的整行)

   -i:不區分大小寫,也可以使用--ignore-case長選項

   -E:支援擴充的正規表達式,相當于egrep

   -r: 連帶檔案夾以下目錄也查找

   -A:與數字一起使用,顯示被模式比對到的行并且顯示被比對到的行的下面多少行

   -B: 與數字一起使用,顯示被模式比對到的行并且顯示被比對到的行的上面多少行

   -C: 與數字一起使用,顯示被模式比對到的行并且顯示被比對到的行的上查找下各顯示多少行

二、正規表達式的使用

1.正規表達式的解釋

   正規表達式是一類字元所書寫的模式,是由一些ASCII碼類字元和一些元字元組成。正規表達式工作在貪婪模式下,盡可能多的去比對字元。

   元字元不表示字元本身的意義,而是用于額外功能性的描述

2.正規表達式的分類

   正規表達式從功能上可以分為基本正規表達式和擴充正規表達式

3.基本正規表達式中元字元的使用

   1)字元比對

      .:表示比對任意一個字元

      案例:要求顯示/etc/passwd中以s和h之間出現任意字元的

1

2

<code>[root@localhost ~]# grep </code><code>"s.h"</code> <code>/etc/passwd</code>

<code>sshd:x:</code><code>74</code><code>:</code><code>74</code><code>:Privilege-separated SSH:/</code><code>var</code><code>/empty/sshd:/sbin/nologin</code>

      []:比對指定範圍内的單個字元

      案例:要求顯示/etc/fstab中出現c和C的行

3

4

5

6

<code>[root@localhost ~]# grep </code><code>"[cC]"</code> <code>/etc/fstab</code>

<code># /etc/fstab</code>

<code># Created by anaconda on Mon Feb </code><code>10</code> <code>10</code><code>:</code><code>21</code><code>:</code><code>28</code> <code>2014</code>

<code># Accessible filesystems, by reference, are maintained under </code><code>'/dev/disk'</code>

<code>UUID=58894bb8-5b23-4f00-baff-0c19d450bfe9 /boot                   ext4    defaults        </code><code>1</code> <code>2</code>

<code>proc                    /proc                   proc    defaults        </code><code>0</code> <code>0</code>

      [^]:比對指定範圍外的單個字元,也可使使用選項v來完成

      案例,顯示/etc/passwd中沒有出現#的行

7

8

9

10

<code>[root@localhost ~]# grep -v </code><code>'#'</code> <code>/etc/fstab</code>

<code>/dev/mapper/vg0-root    /                       ext4    defaults        </code><code>1</code> <code>1</code>

<code>/dev/mapper/vg0-usr     /usr                    ext4    defaults        </code><code>1</code> <code>2</code>

<code>/dev/mapper/vg0-</code><code>var</code>     <code>/</code><code>var</code>                    <code>ext4    defaults        </code><code>1</code> <code>2</code>

<code>/dev/mapper/vg0-swap    swap                    swap    defaults        </code><code>0</code> <code>0</code>

<code>tmpfs                   /dev/shm                tmpfs   defaults        </code><code>0</code> <code>0</code>

<code>devpts                  /dev/pts                devpts  gid=</code><code>5</code><code>,mode=</code><code>620</code>  <code>0</code> <code>0</code>

<code>sysfs                   /sys                    sysfs   defaults        </code><code>0</code> <code>0</code>

      指定範圍的字元還可以使用一些别的元素組成,如

      [[:space:]]:表示空白字元

      [[:lower:]]:表示所有小寫字母

      [[:upper:]]:表示所有大寫字母

      [[:aplha:]]:表示所有字母,也可使用[a-z]表示

      [[:digit:]]:表示所有的數字,也可以使用[0-9]表示

      [[:almun:]]:表示所有的數字和字母

      [[:punct:]]:表示所有特殊字元

   2)次數比對:用來指定比對其前面字元出現的次數,隻能比對模式緊靠着的一個字元

       *:出現任意次

      .*: 出現任意次的任意字元

      \?: 出現0次或者1次

      \{m\}: 表示出現m次

      \{m,n\}: 表示至少出現m次,至多出現n次

      \{m,\}: 表示最少出現m次

      \{0,n\}:表示至多出現n次,其中0不能省略

     案例:顯示/etc/passwd中r後面出現最少一次,最多2次的

<code>[root@localhost ~]# grep </code><code>"ro\{1,2\}"</code> <code>/etc/passwd</code>

<code>root:x:</code><code>0</code><code>:</code><code>0</code><code>:root:/root:/bin/bash</code>

<code>operator:x:</code><code>11</code><code>:</code><code>0</code><code>:operator:/root:/sbin/nologin</code>

<code>rtkit:x:</code><code>499</code><code>:</code><code>497</code><code>:RealtimeKit:/proc:/sbin/nologin</code>

   3)位置錨定符:用于來指定字元出現的位置

       ^:用于錨定行首,用法為^Char

       $:用于錨定行尾,用法為Char$

       ^$:用來表示空白行

    案例:統計/etc/rc.d/rc.sysinit中出現的空白行

<code>[root@localhost ~]# grep </code><code>"^$"</code> <code>/etc/rc.d/rc.sysinit |wc -l</code>

<code>96</code>

   4)單詞的錨定:所有非自然的單詞

       \&lt;:用于錨定單詞的詞首,也可以使用\b表示,用法為\&lt;Char或\bChar

       \&gt;:用于錨定單詞的行尾,也可以使用\b表示,用法為Char\&gt;或Char\b

   案例:顯示/etc/passwd中root的行

<code>[root@localhost ~]# grep </code><code>"\&lt;root\&gt;"</code> <code>/etc/passwd</code>

   5) 分組:對模式進行分組

       \(\)  

   6) 引用:對分組的字元串基于位置引用

       \1: 後向引用,表示引用前面的第一個左括号與之對應的右括号中的模式所比對到的内容

       \2: 表示引用前面的第二個左括号與之對應的右括号中的模式所比對到的内容

       ...

      例:此處不适合于英文狀态,隻是做執行個體分析

       He like his lover

       She love her liker

       He loves his lover

       She like his liker

      要求:前面顯示like的後面顯示liker,前面顯示love的後面顯示lover

<code>[root@localhost ~]# grep </code><code>"\(l..e\).*\1r"</code> <code>i.txt</code>

<code>He loves his lover</code>

<code>She like his liker</code>

4.擴充正規表達式的使用

    1)字元比對

     []:比對指定範圍内的單個字元

     [^]:比對指定範圍外的單個字元,也可使使用選項v來完成

     指定範圍的字元還可以使用一些别的元素組成,如

      ?: 出現0次或者1次

      +: 至少出現1次

      {m}: 表示出現m次

      {m,n}: 表示至少出現m次,至多出現n次

      {m,}: 表示最少出現m次

      {0,n}:表示至多出現n次,其中0不能省略

       \(\)

三、egrep的使用

   egrep相當于grep -E,使用擴充正規表達式來構模組化式,此處不再累贅

   由于本人水準有限,如出現錯誤,請大家多多指正

練習:

1、顯示/proc/meminfo檔案中以大小寫s開頭的行;

# grep "^[sS]" /proc/meminfo

# grep -i "^s" /proc/meminfo

2、取出預設shell為非bash的使用者;

# grep -v "bash$" /etc/passwd | cut -d: -f1

3、取出預設shell為bash的且其ID号最大的使用者;

# grep "bash$" /etc/passwd | sort -n -t: -k3 | tail -1 | cut -d: -f1

4、顯示/etc/rc.d/rc.sysinit檔案中,以#開頭,後面跟至少一個空白字元,而後又有至少一個非空白字元的行;

# grep "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}" /etc/rc.d/rc.sysinit

5、顯示/boot/grub/grub.conf中以至少一個空白字元開頭的行;

# grep "^[[:space:]]\{1,\}[^[:space:]]\{1,\}" /boot/grub/grub.conf

6、找出/etc/passwd檔案中一位數或兩位數;

# grep --color=auto "\&lt;[0-9]\{1,2\}\&gt;" /etc/passwd

7、檢視目前系統上root使用者的所有資訊;

# grep "^root\&gt;" /etc/passwd

8、添加使用者bash和testbash、basher,而後找出目前系統上其使用者名和預設shell相同的使用者;

# grep --color=auto "^\([[:alnum:]]\{1,\}\)\&gt;.*\1$" /etc/passwd

本文轉自wangfeng7399 51CTO部落格,原文連結:http://blog.51cto.com/wangfeng7399/1360804,如需轉載請自行聯系原作者