1. 程式人生 > >正則介紹grep

正則介紹grep

linux

正則表達式,又稱規則表達式,英文名為Regular Expression,在代碼中常簡寫為regex、regexp或RE,是計算機科學的一個概念。正則表通常被用來檢索、替換那些符合某個模式(規則)的文本。


正則表達式是對字符串(包括普通字符(例如,a 到 z 之間的字母)和特殊字符(稱為“元字符”))操作的一種邏輯公式,就是用事先定義好的一些特定字符、及這些特定字符的組合,組成一個“規則字符串”,這個“規則字符串”用來表達對字符串的一種過濾邏輯。正則表達式是一種文本模式,模式描述在搜索文本時要匹配的一個或多個字符串。



grep工具

該命令的格式為:grep [-cinvABC]‘word’ filename,其常用的選項如下所示。

-c 表示打印符合要求的行數。

-i 表示忽略大小寫。

-n 表示輸出符合要求的行及其行號。

-v 表示打印不符合要求的行。

-A 後面跟一個數字(有無空格都可以),例如-A2表示打印符合要求的行以及下面兩行。

-B後面跟一個數字,例如-B2表示打印符合要求的行以及上面兩行。

-C 後面跟一個數字,例如-C2表示打印符合要求的行以及上下各兩行。



過濾出帶有某個關鍵詞的行,並輸出行號

示例命令如下:

#grep –n ‘root’ /etc/passwd

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

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



過濾出不帶有某個關鍵詞的行,並輸出行號

示例命令如下:

#grep –nv ‘root’ /etc/passwd

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

6:sync:x:5:0:sync:/sbin:/bin/sync



過濾出所有包含數字的行

示例命令如下:

#grep ‘[0-9]’ /etc/inittab

#multi-user.target:analogous to runlevel 3

#graphical.target:analogous to runlevel 5



過濾出所有不包含數字的行

示例命令如下:

#grep –v ‘[0-9]’ /etc/inittab



過濾掉所有以#開頭的行

示例命令如下:

#cat /etc/sos.conf

[plugins]


[tunables]


#rpm.rpmva = off

#general.syslogsize = 15


#grep –v ‘^#’ /etc/sos.conf

[plugins]


[tunables]



過濾掉所有空行和以#開頭的行

示例命令如下:

#grep –v ‘^#’ /etc/sos.conf |grep –v ‘^#’

[plugins]

[tunables]



過濾出任意一個字符和重復字符

示例命令如下:

#grep ‘r.o’ /etc/passwd

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

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

.表示任意一個字符。上例中,r.o表示把r與o之間有一個任意字符的行過濾出來。


#grep ‘ooo*’ /etc/passwd

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

*表示零個或多個*前面的字符。上例中,ooo*表示oo、ooo、oooo……或更多的o。



指定要過濾出的字符出現次數

示例命令如下:

#grep ‘o\{2\}’ /etc/passwd

符號{ },其內部為數字,表示前面的字符要重復的次數。需要強調的是,{}作用都需要加上轉義字符\。另外使用“{ }”還可以表示一個範圍,具體格式為{n1,n2},其中n1<n2,表示重復n1到n2次前面的字符,n2還可以為空,這時表示大於等於n1次。



egrep工具

egrep 是grep的擴展版本,可以完成grep不能完成的工作。下面介紹egrep不同於grep的幾個用法。為了試驗方便,先編輯一個test.txt,其內容如下:

rot:x:0:0:rot: /bin/bash

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

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

roooot:x:0:0:rooooot: /bin/bash

1111111111111111111111111111

aaaaaaaaaaaaaaaaaaaaaaaaa



過濾出一個或多個指定的字符

示例命令如下:

#egrep ‘o+’ test.txt

rot:x:0:0:rot: /bin/bash

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

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

roooot:x:0:0:rooooot: /bin/bash

#egrep ‘oo+’ test.txt

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

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

roooot:x:0:0:rooooot: /bin/bash

#egrep ‘ooo+’ test.txt

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

roooot:x:0:0:rooooot: /bin/bash

和grep不同,這裏egrep使用的是符號+,它表示匹配1個或多個+前面的字符,這個“+”是不支持被grep直接使用的。包括上面的{},而egrep可以,而不用加\轉義。示例如下:

#egrep ‘o{2}’ /etc/passwd

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



過濾出零個或一個指定的字符

示例命令如下:

#egrep ‘o?’ test.txt

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

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

roooot:x:0:0:rooooot: /bin/bash

1111111111111111111111111111

aaaaaaaaaaaaaaaaaaaaaaaaa

#egrep ‘ooo?’ test.txt

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

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

roooot:x:0:0:rooooot: /bin/bash


#egrep ‘oooo?’ test.txt

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

roooot:x:0:0:rooooot: /bin/bash



過濾出字符串1或字串符2

示例命令如下:

#egrep ‘aaa|111|ooo’ test.txt

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

roooot:x:0:0:rooooot: /bin/bash

1111111111111111111111111111

aaaaaaaaaaaaaaaaaaaaaaaaa



egrep中()的應用

示例命令如下:

#egrep ‘r(oo|at)o’ test.txt

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

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

roooot:x:0:0:rooooot: /bin/bash

這裏用()表示一個整體,上例中會把包含rooot或者rato的行過濾出來,另外也可以把()和其他符號組合在一起,例如(oo)+就表示1個或者多個oo。如下所示:

#egrep ‘r(oo)+’ test.txt

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

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

roooot:x:0:0:rooooot: /bin/bash


正則介紹grep