天天看點

linux正規表達式grep與egrep

正規表達式:它是指一個用來描述或者比對一系列符合某個句法規則的字元串的單個字元串。在很多文本編輯器或其他工具裡,正規表達式通常被用來檢索或替換那些符合某個模式的文本内容。

其實正規表達式,隻是一種思想,一種表示方法。隻要我們使用的工具支援表示這種思想那麼這個工具就可以處理正規表達式的字元串。常用的工具有grep, sed, awk,這三個都是針對文本的行才操作的。

grep   過濾器

文法: grep  [-cinvABC]  'word'  filename 

-n    顯示行号

-c    count統計符合要求的行數

-v    取反,不包含所選字元的

-i    不區分大小寫

-r    會把目錄下面所有的檔案周遊  例如: grep -r 'root' ./

-A    後面跟數字,A2表示列印符合要求的行及下面二行

-B    後面跟數字,B2表示列印符合要求的行及上面二行

-C    後面跟數字,C2表示列印符合要求的行及上下各二行

^     行首,開頭

$     行尾,結尾

空行用 ^$ 表示

可以做一個别名alias grep="grep --color" 寫入到.bashrc裡面;以後輸入grep指令時查找的關鍵字元會顔色顯示,友善區分。

過濾帶有某個關鍵詞的行并輸出行号,顔色顯示關鍵詞

1

2

3

4

5

<code>[root@localhost ~]</code><code># grep -n --color 'root' passwd </code>

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

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

<code>[root@localhost ~]</code><code># grep -o --color 'root' passwd | wc -l</code>

<code>4</code>

加-o 統計包含關鍵詞的個數;

過濾不帶有某個關鍵詞的行,并輸出行号;

6

7

<code>[root@yonglinux ~]</code><code># grep -nv 'nologin' /etc/passwd</code>

<code>6:</code><code>sync</code><code>:x:5:0:</code><code>sync</code><code>:</code><code>/sbin</code><code>:</code><code>/bin/sync</code>

<code>7:</code><code>shutdown</code><code>:x:6:0:</code><code>shutdown</code><code>:</code><code>/sbin</code><code>:</code><code>/sbin/shutdown</code>

<code>8:halt:x:7:0:halt:</code><code>/sbin</code><code>:</code><code>/sbin/halt</code>

<code>20:user1:x:600:501::</code><code>/home/user1</code><code>:</code><code>/bin/bash</code>

<code>23:mysql:x:27:27:MySQL Server:</code><code>/var/lib/mysql</code><code>:</code><code>/bin/bash</code>

過濾以nologin結尾的,系統禁止登陸的所有使用者;

<code>[root@localhost ~]</code><code># grep 'nologin$' /etc/passwd</code>

<code>bin:x:1:1:bin:</code><code>/bin</code><code>:</code><code>/sbin/nologin</code>

<code>daemon:x:2:2:daemon:</code><code>/sbin</code><code>:</code><code>/sbin/nologin</code>

<code>adm:x:3:4:adm:</code><code>/var/adm</code><code>:</code><code>/sbin/nologin</code>

<code>lp:x:4:7:lp:</code><code>/var/spool/lpd</code><code>:</code><code>/sbin/nologin</code>

<code>mail:x:8:12:mail:</code><code>/var/spool/mail</code><code>:</code><code>/sbin/nologin</code>

<code>uucp:x:10:14:uucp:</code><code>/var/spool/uucp</code><code>:</code><code>/sbin/nologin</code>

示例,列印關鍵字halt所在行的A2 B2 C2

8

9

10

11

12

13

14

<code>[root@yonglinux ~]</code><code># grep -A2 'halt' passwd </code>

<code>halt:x:7:0:halt:</code><code>/sbin</code><code>:</code><code>/sbin/halt</code>

<code>[root@yonglinux ~]</code><code># grep -B2 'halt' passwd </code>

<code>sync</code><code>:x:5:0:</code><code>sync</code><code>:</code><code>/sbin</code><code>:</code><code>/bin/sync</code>

<code>shutdown</code><code>:x:6:0:</code><code>shutdown</code><code>:</code><code>/sbin</code><code>:</code><code>/sbin/shutdown</code>

<code>[root@yonglinux ~]</code><code># grep -C2 'halt' passwd </code>

把所有以#号開頭的行去除

<code>[root@yonglinux ~]</code><code># grep -v '^#' /etc/inittab </code>

<code>id</code><code>:3:initdefault:</code>

去除所有空行和以#号開頭的行

<code>[root@yonglinux ~]</code><code># grep -v '^#' /etc/crontab |grep -v '^$'</code>

<code>SHELL=</code><code>/bin/bash</code>

<code>PATH=</code><code>/sbin</code><code>:</code><code>/bin</code><code>:</code><code>/usr/sbin</code><code>:</code><code>/usr/bin</code>

<code>MAILTO=root</code>

<code>HOME=/</code>

示例說明,列印數字或字母開頭,及不是字母和數字開頭的;

<code>[root@yonglinux tmp]</code><code># cat test.txt </code>

<code>helloworld</code>

<code>abc</code>

<code>abc11111</code>

<code>#differt</code>

<code>12345</code>

<code>67899</code>

<code>123def</code>

[0-9]代表任意一個數字,整個指令意思篩選出包含任意一個數字的行;

<code>[root@yonglinux tmp]</code><code># grep '[0-9]' test.txt </code>

[^0-9]代表除0-9之外的任意一個字元,整個指令的意思是篩選出不包含數字的行;

<code>[root@yonglinux tmp]</code><code># grep '[^0-9]' test.txt </code>

^[^0-9]代表不是數字開頭的;

<code>[root@yonglinux tmp]</code><code># grep '^[^0-9]' test.txt </code>

[a-z]代表任意一個英文字母;

<code>[root@yonglinux tmp]</code><code># grep '[a-z]' test.txt </code>

[^a-z]代表除英文字母以外的;

<code>[root@yonglinux tmp]</code><code># grep '[^a-z]' test.txt </code>

^[^a-z]代表不是英文字母開頭的文本;

<code>[root@yonglinux tmp]</code><code># grep '^[^a-z]' test.txt </code>

[ ] 如果是數字的話就用[0-9]這樣的形式,當然有時候也可以用這樣的形式[15]即隻含有1或者5,注意,它不會認為是15。如果要過濾出數字以及大小寫字母則要這樣寫[0-9a-zA-Z]。另外[ ]還有一種形式,就是[^字元] 表示除[ ]内的字元之外的字元。

過濾任意一個字元與重複字元

<code>[root@yonglinux ~]</code><code># grep 'h..t' /etc/passwd</code>

'.'點表示任意的一個字元,上面例子為把符合h與t之間有2個任意字元的行過濾出來。

'*'代表零個或多個任意的字元

'ooo*'代表oo,ooo,oooo 或者更多的o

<code>[root@yonglinux ~]</code><code># grep 'ooo*' /etc/passwd</code>

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

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

<code>postfix:x:89:89::</code><code>/var/spool/postfix</code><code>:</code><code>/sbin/nologin</code>

'.*'表示零個或多個任意字元,等于所有的,空行也包含在内。

<code>[root@yonglinux ~]</code><code># grep '.*' /etc/passwd |wc -l</code>

<code>24</code>

<code>[root@yonglinux ~]</code><code># wc -l /etc/passwd</code>

<code>24 </code><code>/etc/passwd</code>

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

{ }内部為數字,表示前面字元要重複的次數。表示兩個O即包含OO的行。{ }左右都需要加脫意字元\ 

grep -E 代表增強版的grep即egrep,使用egrep不需要脫意;

<code>[root@yonglinux ~]</code><code># grep 'o\{2\}' /etc/passwd</code>

<code>[root@localhost ~]</code><code># grep -E 'o{2}' passwd </code>

<code>[root@localhost ~]</code><code># egrep 'o{2}' passwd</code>

<code>[root@yonglinux ~]</code><code># cat test.txt </code>

<code>root:hot</code>

<code>abcde</code>

<code>spoool</code>

<code>spool</code>

<code>spol</code>

<code>spl</code>

示例,過濾字母o出現1到3次的行

<code>[root@yonglinux ~]</code><code># grep 'o\{1,3\}' test.txt </code>

{ } 還可以表示一個範圍,格式為{n1,n2} n1&lt;n2 表示重複n1到n2次前面的字元,n2還可以為空,則表示大于等于n1次。

egrep為grep的擴充版本,我們可以用egrep完成grep不能完成的工作,當然了grep能完成的egrep完全可以完成。

grep -E = egrep

1、篩選一個或一個以上前面的字元    字元後面使用+

15

16

17

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

<code>rooooot:x:0:0</code><code>/roooooot</code><code>:</code><code>/bin/bash</code>

<code>11111111111111111111111111111111</code>

<code>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</code>

<code>[root@yonglinux ~]</code><code># egrep 'o+' test.txt </code>

<code>[root@yonglinux ~]</code><code># egrep 'oo+' test.txt </code>

<code>[root@yonglinux ~]</code><code># egrep 'ooo+' test.txt </code>

2、篩選零個或一個前面的字元    字元後面使用?

<code>[root@yonglinux ~]</code><code># egrep 'o?' test.txt </code>

<code>[root@yonglinux ~]</code><code># egrep 'oo?' test.txt </code>

<code>[root@yonglinux ~]</code><code># egrep 'ooo?' test.txt </code>

<code>[root@yonglinux ~]</code><code># egrep 'oooo?' test.txt </code>

3、篩選字元串1或字元串2    包含裡面任意一個字元串的列印出來

<code>[root@yonglinux ~]</code><code># egrep 'aaa|111|ooo' test.txt </code>

4、egrep中()的應用

<code>[root@yonglinux ~]</code><code># egrep 'r(oo)|(mo)n' test.txt </code>

用( )表示一個整體,例如(oo)+ 表示1個'oo'或者多個'oo'

<code>[root@yonglinux ~]</code><code># egrep '(oo)+' test.txt </code>

5、egrep中[ ]的應用    

方括号内的字元為其中的一個;[^o]為除了字母o之外的;

示例:r開頭t結尾的;;

<code>[root@localhost ~]</code><code># egrep 'r[o]t' test.txt </code>

r開頭後面有o的

<code>[root@localhost ~]</code><code># egrep 'r[o]' test.txt </code>

r開頭後面不是o的;

<code>[root@localhost ~]</code><code># egrep 'r[^o]' test.txt </code>

<code>rrt</code>

<code>rtx</code>

t為結尾的前面字元不是o的;

<code>[root@localhost ~]</code><code># egrep '[^o]t' test.txt </code>

. * + ? 符号的總結

.    表示任意一個字元(包括特殊字元 空格 # $ ?)

*    表示零個或多個*前面的字元

.*   表示任意個任意字元(包含空行)

+    表示1個或多個+前面的字元

?    表示0個或1個?前面的字元

其中,+ ? grep不支援,egrep才支援。

"ro.*t" 表示以ro開頭一直到t結尾的

<code>[root@localhost ~]</code><code># grep 'ro.*t' test.txt </code>

圖檔顯示的更詳細,友善大家了解。

<a href="http://s3.51cto.com/wyfs02/M02/5E/03/wKiom1Uo5D-BaRYIAALsDh09vUE461.jpg" target="_blank"></a>

grep如果需要篩選字元串 | 管道需要加脫意\才可以使用;

<a href="http://s3.51cto.com/wyfs02/M00/5D/FF/wKioL1Uo5XWTMk6WAAGvYBkdK6U656.jpg" target="_blank"></a>

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

繼續閱讀