grep [選項] PATTERN [文件]html
說明:grep(global search regular expression(RE) and print out the line,全面搜索正則表達式並把行打印出來)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。linux
在每一個文件或標準輸入中查找PATTERNgit
默認的PATTERN是一個基本正則表達式(縮寫爲BRE)正則表達式
(1).經常使用選項shell
-A,--after-context=NUM 輸出當前行以及向後數NUM數的全部行 -B,--before-context=NUM 輸出當前行以及向前數NUM數的全部行 -C,--context=NUM 輸出當前行以及向前向後各數NUM數的全部行 -c,--count 統計每一個文件中包含指定字符串的行數 -e,--regexp=PATTERN 用PATTERN來進行匹配操做 -E,--extended-regexp PATTERN是一個可擴展的正則表達式(縮寫ERE) -f<範本文件>,--file=FILE 指定範本文件,其內容有一個或多個範本樣式,讓grep查找符合範本條件的文件內容,格式爲每一行的範本樣式 -F,--fixed-strings PATTERN是一個由斷行符分隔的定長字符串(取消默認的基本正則表達式) -h,--no-filename 輸出時不顯示文件名前綴 -i,--ignore-case 忽略大小寫 -l,--files-with-match 只顯示包含指定字符串的文件名 -n,--line-number 輸出的同時顯示行號 -o,--only-matching 只顯示匹配到的字符串 -q,--quiet,--silent 靜默模式,不輸出任何信息 -s,--nomessages 不顯示不存在或沒法匹配的錯誤信息 -v,--invert-march 顯示不匹配的行 -V,-version 顯示版本信息 -w,--word-regexp 匹配整個單詞 --color=auto 對匹配到的文本着色顯示
(2).瞭解基本正則表達式和擴展正則表達式express
基本正則表達式(BRE)bash
元字符分類:字符匹配、匹配次數、位置錨定、分組dom
i. 字符匹配: 工具
. 匹配任意單個字符 # 須要匹配「.」字符時,用 \. 或 [.] 表示ui
[] 匹配指定範圍內的任意單個字符
[^] 匹配指定範圍外的任意單個字符
[:alnum:] 字母和數字
[:alpha:] 表明任何英文大小寫字符,亦即 A-Z, a-z
[:lower:] 小寫字母 [:upper:] 大寫字母
[:blank:] 空白字符(空格和製表符)
[:space:] 水平和垂直的空白字符(比[:blank:]包含的範圍廣)
[:cntrl:] 不可打印的控制字符(退格、刪除、警鈴...)
[:digit:] 十進制數字 [:xdigit:]十六進制數字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 標點符號
ii. 匹配次數:用在要指定次數的字符後面,用於指定前面的字符要出現的次數
* 匹配前面的字符任意次,包括0次
貪婪模式:儘量長的匹配
.* 任意長度的任意字符
\? 匹配其前面的字符0或1次
\+ 匹配其前面的字符至少1次 (非貪婪模式,懶惰模式)
\{n\} 匹配前面的字符n次
\{m,n\} 匹配前面的字符至少m次,至多n次
\{,n\} 匹配前面的字符至多n次
\{n,\} 匹配前面的字符至少n次
iii. 位置錨定:定位出現的位置
^ 行首錨定,用於模式的最左側。例:^#,以#開頭
$ 行尾錨定,用於模式的最右側。例:#$,以#結尾
^PATTERN$ 用於模式匹配整行
^$ 空行
^[[:space:]]*$ 空白行
\< 或 \b 詞首錨定,用於單詞模式的左側
\> 或 \b 詞尾錨定;用於單詞模式的右側
\<PATTERN\> 匹配整個單詞
iv. 分組:
\(\) 將一個或多個字符捆綁在一塊兒,看成一個總體進行處理,如:\(root\)\+
分組括號中的模式匹配到的內容會被正則表達式引擎記錄於內部的變量中,這些變量的命名方式爲: \1, \2, \3, ...
\1 表示從左側起第一個左括號以及與之匹配右括號之間的模式所匹配到的字符
示例: \(string1\+\(string2\)*\) \1 :string1\+\(string2\)* \2 :string2
注:後向引用:引用前面的分組括號中的模式所匹配字符,而非模式自己
或者:\|
示例:a\|b: a或b C\|cat: C或cat \(C\|c\)at:Cat或cat
擴展正則表達式(ERE)
擴展正則表達式的元字符:字符匹配、匹配次數、位置錨定、分組 (與基本正則表達式基本相同;用法類似,除了詞首、詞尾錨定同樣,其餘只是在基本正則表達式中去掉轉義字符)
與基本正則表達式的比較:寫法上比較簡單,去掉了大量的轉義字符;但須要匹配特殊字符時,擴展正則表達式需將特殊字符用 [] 括起來使用,這時用基本正則表達式比較方便
格式: egrep [OPTIONS] PATTERN [FILE...]
egrep = grep -E(等價)
i. 字符匹配 (與基本正則表達式基本相同)
ii. 位置錨定:
^ :行首
$ :行尾
\<, \b :語首
\>, \b :語尾
iii. 分組:
()
後向引用:\1, \2, ...
iv. 或者:
|
示例: a|b: a或b C|cat: C或cat (C|c)at:Cat或cat
正則表達式例題:
一、顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
i. cat /proc/meminfo |grep -i "^s"
ii. cat /proc/meminfo |grep "^\(s\|S\)" 注意:\(和\|以及\)都是轉義字符
二、顯示/etc/passwd文件中不以/bin/bash結尾的行
cat /etc/passwd |grep -v ":/bin/bash$"
三、顯示用戶rpc默認的shell程序
cat /etc/passwd |grep -w "^rpc" |cut -d: -f 7
四、找出/etc/passwd中的兩位或三位數
cat /etc/passwd |grep -wo "[[:digit:]]\{2,3\}"
五、顯示CentOS7的/etc/grub2.cfg文件中,至少以一個空白字符開頭的且後面有非空白字符的行
cat /etc/grub2.cfg | grep "^[[:space:]]\+[^' '].*" 或 cat /etc/grub2.cfg | grep "^[[:space:]]\+[^[:space:]].*"
六、找出「netstat -tan」命令結果中以LISTEN後跟任意多個空白字符結尾的行
netstat -tan | grep ".*LISTEN[[:space:]]*$"
七、顯示CentOS7上全部系統用戶的用戶名和UID
cat /etc/passwd |cut -d: -f1,3 |grep -w "[1-9][0-9]\{,2\}$"
八、添加用戶bash、testbash、basher、sh、nologin(其shell爲/sbin/nologin),找出/etc/passwd用戶名和shell同名的行
cat /etc/passwd |grep -w "^\([^:]*\):.*/\1$"
九、利用df和grep,取出磁盤各分區利用率,並從大到小排序
df |grep "^/dev/sd"|grep -wo "[0-9]\+%"|sort -nr
十、顯示三個用戶root、mage、wang的UID和默認shell
cat /etc/passwd |grep -w "^\(root\|mage\|wang\)" |cut -d: -f 3,7
十一、找出/etc/rc.d/init.d/functions文件中行首爲某單詞(包括下劃線)後面跟一個小括號的行
cat /etc/rc.d/init.d/functions |grep -i "^\([_[:alnum:]]\+(\)"
十二、使用egrep取出/etc/rc.d/init.d/functions中其基名
echo "/etc/rc.d/init.d/functions" | grep -Eo "[^/]*[/]?$"|tr -d "/"
1三、使用egrep取出上面路徑的目錄名
echo "/etc/rc.d/init.d/" | grep -Eo "..*[/]\<"
1四、統計last命令中以root登陸的每一個主機IP地址登陸次數
last |grep -w "^root.*\<pts" | grep -wE "((([0-9])|([1-9][0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))[.]){3}(([0-9])|([1-9][0-9])|(1[0-9]{,2})|(2[0-4][0-9])|(25[0-5])){1}[[:space:]]" |tr -s " "|cut -d " " -f3|sort|uniq -c
1五、利用擴展正則表達式分別表示0-九、10-9九、100-19九、200-24九、250-255
[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]
1六、顯示ifconfig命令結果中全部IPv4地址
ifconfig |grep -owE "((([0-9]{1,2})|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))[.]){3}(([0-9]{1,2})|(1[0-9]{,2})|(2[0-4][0-9])|(25[0-5])){1}[[:space:]]"
1七、將此字符串:welcome to magedu linux 中的每一個字符去重並排序,重複次數多的排到前面
cat test | grep -o "[[:lower:]]"|sort |uniq -c|sort -nr |tr -s ' ' | cut -d " " -f 3 |tr -d '\n'
注:匹配有效ip地址:
i. grep -owE "((([0-9])|([1-9][0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))[.]){3}(([0-9])|([1-9][0-9])|(1[0-9]{,2})|(2[0-4][0-9])|(25[0-5])){1}[[:space:]]" -E : 表明擴展正則表達式
ii. grep -owE "((([0-9]{1,2})|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))[.]){3}(([0-9]{1,2})|(1[0-9]{,2})|(2[0-4][0-9])|(25[0-5])){1}[[:space:]]"
(3).實例
在特定的文本集中查找特定字符串
[root@CentOS6 桌面]# cat >t1.txt<<EOF > I'm MenAngel! > Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! > EOF [root@CentOS6 桌面]# cat >t2.txt<<EOF > Every one fights for a better future,but I fight for freedom! > EOF [root@CentOS6 桌面]# cat >t3.txt<<EOF > There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! > When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked. > EOF [root@CentOS6 桌面]# grep "MenAngel" t1.txt t2.txt t3.txt t1.txt:I'm MenAngel!
輸出指定字符串所在行以外的全部文件的行內容
[root@CentOS6 桌面]# grep -v "MenAngel" t1.txt t2.txt t3.txt t1.txt:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! t2.txt:Every one fights for a better future,but I fight for freedom! t3.txt:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! t3.txt:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.
將查找的字符串用特定的顏色標記出來
[root@CentOS6 桌面]# grep "fight" t1.txt t2.txt t3.txt --color=auto t2.txt:Every one fights for a better future,but I fight for freedom!
統計每一個文件中包含指定字符串的行數
是行數,不是總數,一行有兩個指定字符串也只算一次 [root@CentOS6 桌面]# grep -c "that" t1.txt t2.txt t3.txt t1.txt:1 t2.txt:0 t3.txt:2
默認狀況,輸出包含特定字符串所在的行
[root@CentOS6 桌面]# grep -n "that" t1.txt t2.txt t3.txt t1.txt:2:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! t3.txt:1:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! t3.txt:2:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked. [root@CentOS6 桌面]# grep "that" t1.txt t2.txt t3.txt t1.txt:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! t3.txt:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! t3.txt:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.
配合cat查找文件中指定字符串所在的行
[root@CentOS6 桌面]# cat t1.txt t2.txt t3.txt | grep "MenAngel" I'm MenAngel! [root@CentOS6 桌面]# grep "MenAngel" t1.txt t2.txt t3.txt t1.txt:I'm MenAngel!
查找以指定字符串開頭的文本行並輸出
[root@CentOS6 桌面]# cat t1.txt t2.txt t3.txt | grep ^I I'm MenAngel! [root@CentOS6 桌面]# cat t1.txt t2.txt t3.txt | grep ^M //沒有以M開頭的行 [root@CentOS6 桌面]#
輸出不以指定字符串開頭的文本所在行的內容
[root@CentOS6 桌面]# cat t1.txt t2.txt t3.txt | grep ^[^I] Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! Every one fights for a better future,but I fight for freedom! There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.
顯示特定行先後內容
[root@CentOS6 桌面]# seq 10 | grep "5" -C 3 2 3 4 5 6 7 8 [root@CentOS6 桌面]# seq 10 | grep "5" -A 3 5 6 7 8 [root@CentOS6 桌面]# seq 10 | grep "5" -B 3 2 3 4 5
無論特定行先後行數是否足夠,參數都是可用的
[root@CentOS6 桌面]# grep -C 3 "MenAngel" t1.txt t2.txt t3.txt t1.txt:I'm MenAngel! t1.txt-Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world!
若是匹配特定行先後內容有多個結果,會有--分割,但不會重複顯示
[root@CentOS6 桌面]# grep -A 1 "that" t1.txt t2.txt t3.txt t1.txt:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! -- t3.txt:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! t3.txt:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked. [root@CentOS6 桌面]# grep -B 1 "that" t1.txt t2.txt t3.txt t1.txt-I'm MenAngel! t1.txt:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! -- t3.txt:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! t3.txt:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.
匹配多個字符串
[root@CentOS6 桌面]# grep -e "MenAngel" -e "that" -o t1.txt t2.txt t3.txt t1.txt:MenAngel t1.txt:that t3.txt:that t3.txt:that t3.txt:that
匹配樣本文件中每行字符串
[root@CentOS6 桌面]# cat >patfile<<EOF > MenAngel > that > EOF [root@CentOS6 桌面]# grep -f patfile t1.txt t2.txt t3.txt t1.txt:I'm MenAngel! t1.txt:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! t3.txt:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! t3.txt:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.
參考資料:
https://www.cnblogs.com/Dlg-Blog/p/8733908.html
http://www.cnblogs.com/MenAngel/p/5547219.html