"在Linux中正則表達式很重要,在日常使用vim做字處理或程序撰寫都會使用到,簡單的說,正則表達式就是處理字符串的方法它是以行為單位來進行字符串的處理行為, 正規(guī)表示法透過一些特殊符號的輔助,可以讓使用者輕易的達到『搜尋/刪除/取代』某特定字符串的處理程序!
**grep: **Global search REgularexpression and Print out the line
作用:文本搜索工具,根據(jù)用戶指定的“模式”對目標文本逐行進行匹配檢查;打印匹配到的行
模式:由正則表達式字符及文本字符所編寫的過濾條件
語法:grep [options] pattern [file...]
默認一般在管理員下grep給予了別名定義:alias grep='grep --color=auto'及當匹配到了以顏色表示出來。
普通用戶也自己在~/.bashrc里添加
grep 常見的命令選項:
-v:顯示不被pattern匹配到的行
-i:忽略字符大小寫
-n:顯示匹配到的行號
-c:統(tǒng)計匹配到的的行數(shù)
-o:僅顯示匹配到的字符串
-q:靜默模式,不輸出任何信息
-A#:顯示匹配到的行及后#行顯示出來
-B#:顯示匹配到的行及前#行顯示出來
-C#:顯示匹配到的行及前后各#行顯示出來
-e:實現(xiàn)多個選項間的邏輯or關(guān)系
-w:匹配整個單詞
-E:使用ERE (egrep)
-F:及fgrep,不支持正則表達式。
grep的示例:
1.我們以/etc/passwd文本來匹配我們所需的信息:
grep -v “root” /etc/passwd #除了匹配到的帶“root”的行其行行顯示出來
[root@centos7 app]#grep -v "root" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
.......
tcpdump:x:72:72::/:/sbin/nologin
zj:x:1000:1000:zj:/home/zj:/bin/bash
bob:x:1001:1001::/home/bob:/bin/bash
2.grep -i “root” /etc/passwd #顯示匹配到的“root”不分大小寫及“root”“ROOT”都可以
[root@centos7 app]#grep -i "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
rooter:x:1002:1002::/home/rooter:/bin/bash
ROOT:x:1004:1005::/home/ROOT:/bin/bash
3.grep -n "root" /etc/passwd #顯示匹配到“root”的行及顯示行號
[root@centos7 app]#grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
45:rooter:x:1002:1002::/home/rooter:/bin/bash
4.grep -c “root” /etc/passwd #統(tǒng)計匹配到“root”的行數(shù)
[root@centos7 app]#grep -c "root" /etc/passwd
3
5.grep -o “root” /etc/passwd #僅顯示匹配到的“root”字符串
[root@centos7 app]#grep -o "root" /etc/passwd
root
root
root
root
root
root
6.grep -q “root” /etc/passwd #靜默模式,及屏幕上不輸出任何信息
[root@centos7 app]#grep -q "root" /etc/passwd
[root@centos7 app]#grep -q "root" /etc/passwd;echo $?
0
及有時候匹配一個字符我只要知道它成功與否。
7.grep -A 1 “root” /etc/passwd #顯示匹配到“root”的行及后面的1行
[root@centos7 app]#grep -A1 "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
--
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
--
rooter:x:1002:1002::/home/rooter:/bin/bash
lao:x:1003:1004::/home/lao:/bin/bash
8.grep -B 1 “root” /etc/passwd #顯示匹配到“root”的行及前面的1行
[root@centos7 app]#grep -B1 "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
--
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
--
bob:x:1001:1001::/home/bob:/bin/bash
rooter:x:1002:1002::/home/rooter:/bin/bash
9.grep -C 1 “root” /etc/passwd #顯示匹配到“root”的行及前后的1行
[root@centos7 app]#grep -C1 "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
--
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
--
bob:x:1001:1001::/home/bob:/bin/bash
rooter:x:1002:1002::/home/rooter:/bin/bash
lao:x:1003:1004::/home/lao:/bin/bash
10.grep -e “root” -e “adm” /etc/passwd #顯示匹配到“root”和“bin”的行
[root@centos7 app]#grep -e "root" -e "adm" /etc/passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
rooter:x:1002:1002::/home/rooter:/bin/bash
11.grep -w "root" /etc/passwd #顯示匹配到的單詞“root”
[root@centos7 app]#grep -w "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
grep -E 使用ERE即(egrep)
grep -f 相當于fgrep,不支持正則表達式
REGEXP由一類特殊字符及文本字符所編寫的模式,其中有些字符(元字符)不表示字符字面意義,而表示控制或通配的功能
元字符分類:字符匹配、匹配次數(shù)、位置錨定、分組
字符匹配:
[] 匹配指定范圍內(nèi)的任意單個字符
[^] 匹配指定范圍外的任意單個字符
[:alnum:] 字母和數(shù)字
[:alpha:] 代表任何英文大小寫字符,亦即A-Z, a-z
[:lower:] 小寫字母[:upper:] 大寫字母
[:blank:] 空白字符(空格和制表符)
[:space:]水平和垂直的空白字符(比[:blank:]包含的范圍廣)
[:cntrl:] 不可打印的控制字符(退格、刪除、警鈴...)
[:digit:] 十進制數(shù)字[:xdigit:]十六進制數(shù)字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 標點符號```
**匹配次數(shù)**:用在要指定次數(shù)的字符后面,用于指定前面的字符要出現(xiàn)的次數(shù)
```* 匹配前面的字符任意次,包括0次
貪婪模式:盡可能長的匹配
.*任意長度的任意字符
\?匹配其前面的字符0或1次
\+匹配其前面的字符至少1次
\{n\}匹配前面的字符n次
\{m,n\}匹配前面的字符至少m次,至多n次
\{,n\}匹配前面的字符至多n次
\{n,\}匹配前面的字符至少n次```
**位置錨定:**
``` ^ 行首錨定,用于模式的最左側(cè)
$ 行尾錨定,用于模式的最右側(cè)
^PATTERN$ 用于模式匹配整行
^$ 空行
^[[:space:]]*$ 空白行
\< 或\b詞首錨定,用于單詞模式的左側(cè)
\> 或\b詞尾錨定;用于單詞模式的右側(cè)
\<PATTERN\>匹配整個單詞```
**分組:**\(\) 將一個或多個字符捆綁在一起,當作一個整體進行處理,如:\(root\)\+
```分組括號中的模式匹配到的內(nèi)容會被正則表達式引擎記錄于內(nèi)部的變量中,這些變量的命名方式為: \1, \2, \3, ...
\1表示從左側(cè)起第一個左括號以及與之匹配右括號之間的模式所匹配到的字符
示例:\(string1\+\(string2\)*\)
\1 :string1\+\(string2\)*
\2 :string2```
**后向引用:**引用前面的分組括號中的模式所匹配字符,而非模式本身
**或者:**\|
```示例:a\|b: a或b C\|cat: C或cat \(C\|c\)at:Cat或cat```
##egrep
egrep用法與grep 相差不大
egrep=grep -E
egrep[OPTIONS] PATTERN [FILE...]
擴展正則表達式的元字符:
**字符匹配:**
```. 任意單個字符
[] 指定范圍的字符
[^] 不在指定范圍的字符```
**次數(shù)匹配:**
```*:匹配前面字符任意次
?: 0或1次
+:1次或多次
{m}:匹配m次
{m,n}:至少m,至多n次```
**位置錨定:**
```^ :行首
$ :行尾
\<, \b :語首
\>, \b :語尾```
**分組:**
```()
后向引用:\1, \2, ...```
**或者:**
```a|b: a或b
C|cat: C或cat
(C|c)at:Cat或cat```

通過上表我們可以看出egrep的使用比grep簡單點,過程沒那么繁瑣。
例:http://www.lxweimin.com/p/a046dfbd0edd