標籤:學習記錄
一)、Linux的三大文本處理之GREP:
GREP(Global search REgular expression and Print out the line,全面搜尋Regex並把行列印出來):在給出檔案清單或標準輸出後,grep會對匹配一個或多個Regex的文本進行搜尋,並只輸出匹配(或者不匹配)的行或文本。----摘自於維基百科
GREP家族包括:
grep:
egrep:支援擴充Regex的grep,等同於grep -E。
fgrep:fixed grep或fast grep,不支援Regex。
二)、文法格式:
grep [option]... ‘PATTERN‘ FILE...
注意:以字串逐行尋找。
常用命令選項:
-v:反向選取,即顯示不匹配的行。
-o:僅顯示匹配的字串,而非字串所在的行。
-i:ignore-case,忽略字元大小寫。
-E:支援使用擴充Regex。
-A #:顯示匹配行及下#行。
-B #:顯示匹配行及上#行。
-C #:顯示匹配行及上下#行。
--color=auto:匹配到的字串顯示顏色。
三)、Regex:
Regular Expression,在代碼中常簡寫為regex、regexp或RE,電腦科學的一個概念。Regex使用單個字串來描述、匹配一系列符合某個句法規則的字串。在很多文字編輯器裡,Regex通常被用來檢索、替換那些符合某個模式的文本。----摘自於維基百科
四)、元字元:
1)、字元匹配:
.:匹配任意一個單個字元
[]:匹配一個指定範圍內的字元
[^]:匹配一個不在指定範圍內的字元
[:digit:]:數字字元,即[0-9]
[:lower:]:小寫字元,即[a-z]
[:upper:]:大寫字元,即[A-Z]
[:alpha:]:文字字元,即[A-Z,a-z]
[:alnum:]:文字數字字元,即[A-Z,a-z,0-9]
[:xdigit:]:十六進位數字,即[0-9,a-f,A-F]
[:punct:]:標點符號
[:space:]:空白字元
[:graph:]:非空白字元
[:cntrl:]:控制字元
[:print:]:非Null 字元(包括空格)
2)、次數匹配:
*:零次或多次先前字元。
.*:任意次任一字元
\?:零次或一次先前字元。
\{m\}:先前字元重複m次。
\{m,n\}:先前字元m到n次。
x\{m,\}:先前字元至少m次。
x\{0,n\}:先前字元至多n次。
3)、位置錨定:
^:行首錨定。
$:行尾錨定。
^$:空白行。
\<,/b:錨定詞首,出現於單詞左側。
\>,\b:錨定詞尾,出現於單詞右側。
\<..\>,\b..\b:單詞鎖定符。
4)、分組和引用:
\(..\):
分組中的模式比對到的內容,可由Regex引擎記憶在記憶體中,之後可被引用。
\#:
引用第n個括弧所匹配到的內容,而非模式本身。
五)、擴充Regex
1)、字元匹配:
與基本Regex相同。
2)、次數匹配
與基本Regex相同,且無需轉譯。
+:一次或多次先前字元。
a|b:分支,a或b,
con(C|c)at:concat或conCat
conC|cat:conC或cat
3)、位置錨定:
與基本Regex相同。
4)、分組和引用
與基本Regex相同,分組無需轉譯。
六)、執行個體說明:
01、顯示/proc/meminfo檔案中以大寫或小寫S開頭的行。
#grep --color=auto -i ‘^s‘ /proc/meminfo#grep --color=auto -E ‘^(s|S)‘ /proc/meminfo
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150518/1431959526140463.jpg" title="1431959526140463.jpg" alt="1.jpg" />
02、顯示/etc/passwd檔案中其預設shell為非/sbin/nologin的使用者。
#grep --color=auto -v "/sbin/nologin$" /etc/passwd | cut -d: -f1
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150518/1431959858169135.jpg" title="1431959858169135.jpg" alt="2.jpg" />
03、顯示/etc/passwd檔案中其預設shell為/bin/bash的使用者;
進一步:僅顯示上述結果中其ID號最大的使用者。
#grep --color=auto "/bin/bash$" /etc/passwd | sort -t: -k3 -n | tail -n 1 | cut -d: -f1,3,7
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150518/1431960190164226.jpg" title="1431960190164226.jpg" alt="3.jpg" />
04、找出/etc/passwd檔案中的一位元或兩位元。
#grep --color=auto "\<[[:digit:]]\{1,2\}\>" /etc/passwd
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150518/1431960515911997.jpg" title="1431960515911997.jpg" alt="4.jpg" />
05、顯示/boot/grub/grub.conf中以至少一個空白字元開頭的行。
#grep --color=auto "^[[:space:]]\{1,}" /boot/grub/grub.conf
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150518/1431962706890926.jpg" title="1431962706890926.jpg" alt="5.jpg" />
06、顯示/etc/rc.d/rc.sysinit檔案中,以#開頭,後面跟至少一個空白字元,而後又有至少一個非空白字元的行。
#grep --color=auto "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}" /etc/rc.d/rc.sysinit
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150518/1431960981386967.jpg" title="1431960981386967.jpg" alt="6.jpg" />
07、找出netstat -tan命令執行結果中以‘LISTEN‘結尾的行。
#netstat -tan | grep --color=auto "LISTEN[[:space:]]*$"
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150518/1431961263115812.jpg" title="1431961263115812.jpg" alt="7.jpg" />
08、添加使用者bash、testbash、basher、nologin(SHELL為/sbin/nologin),而找出當前系統上其使用者名稱和預設shell相同的使用者。
#grep --color=auto "^\([[:alpha:]]\{1,\}\):.*/\1$" /etc/passwd
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150518/1431962021113971.jpg" title="1431962021113971.jpg" alt="8.jpg" />
09、顯示當前系統上root、rhel或centos使用者的預設shell。
#grep --color=auto -E "^(root|rhel|centos):.*" /etc/passwd | cut -d: -f1,7
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150518/1431962937111020.jpg" title="1431962937111020.jpg" alt="9.jpg" />
10、找出/etc/rc.d/init.d/functions檔案中某單詞後跟一組小括弧"()"行
#grep --color=auto -E "\<[[:alpha:]]+\>\(\)" /etc/rc.d/init.d/functions
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150518/1431963348651501.jpg" title="1431963348651501.jpg" alt="10.jpg" />
11、找出ifconfig命令結果中的1-255之間的數字;
#ifconfig | grep --color=auto -E "\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"
650) this.width=650;" src="http://www.178linux.com/ueditor/php/upload/image/20150519/1431965057125437.jpg" title="1431965057125437.jpg" alt="11.jpg" />
本文出自 “小馬的學習記錄” 部落格,請務必保留此出處http://masachencer.blog.51cto.com/8683770/1652621
Linux的文本處理工具grep及初識Regex