Linux三劍客之老三grep
說明:
Linux系統中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。工作中我們常常用它來過濾出我們想要的數據。
格式:
grep [OPTIONS]
基本參數:
-i 不區分大小寫
-v 排除內容,即取反
-n 對匹配到的內容打印相應行號
-E 使用擴展正則表達式(相當於egrep)
-r 遞歸讀取目錄下的文件(即包括子目錄下文件)
-c 對匹配到的行進行計數
-o 只顯示匹配到的內容
-A (after)匹配輸出內容行並輸出內容行後的指定行
-B (before)匹配輸出內容行並輸出內容行前的指定行
-C (context)匹配輸出內容行並輸出內容行的前後指定行
--color 過濾的內容顯示顏色
建議配置別名:
echo “alias grep=‘grep --color‘” >> /etc/profile //配置別名 source /etc/profile //重讀文件使別名生效
效果:
[root@vm1 test]# grep ".*" /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin
常見用法:
① grep -ivnEoc… “匹配內容” 文件名
[root@vm1 test]# grep -n "root" /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 11:operator:x:11:0:operator:/root:/sbin/nologin
② grep -r “匹配內容” 文件目錄/*
[root@vm1 ~]# grep -r "www" test/* test/Caiyun.txt:My blog is http://www.cnblogs.com/Caiyundo/ test/dudu/3.txt:www.cnblog.com/caiyun
③ grep “匹配內容” -A 指定行 文件名
[root@vm1 ~]# grep "root:x" -nA 5 /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 2-bin:x:1:1:bin:/bin:/sbin/nologin 3-daemon:x:2:2:daemon:/sbin:/sbin/nologin 4-adm:x:3:4:adm:/var/adm:/sbin/nologin 5-lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6-sync:x:5:0:sync:/sbin:/bin/sync
④ 命令 | grep “匹配內容”
[root@vm1 ~]# ps -ef |grep "sshd" root 1261 1 0 Dec12 ? 00:00:00 /usr/sbin/sshd
擴展:
正則表達式是為處理大量字符串而定義的一套規則和方法,linux正則表達式常用於linux三劍客中。
測試場景模擬(直接復制粘貼即可):
cat >>text.txt<<EOF My name is Caiyun. I like badminton, snooker, running Maybe I‘m not a smart person, but I‘m working hard. My blog is http://www.cnblogs.com/Caiyundo/ Welcome to my blog! CAIYUN My qq is 791111890 My mail is [email protected]. EOFView Code
一、 基礎正則表達式
^ 表示以...字符開頭,^word
[root@vm1 test]# grep -n "^My" text.txt 1:My name is Caiyun. 5:My blog is http://www.cnblogs.com/Caiyundo/ 8:My qq is 791111890 9:My mail is [email protected].
$ 表示以...字符結尾,word$
[root@vm1 test]# grep -n "\.$" text.txt 1:My name is Caiyun. 4:Maybe I‘m not a smart person, but I‘m working hard. 9:My mail is [email protected].
^$ 表示空字符,即可理解為空行
[root@vm1 test]# grep -n "^$" text.txt 3: 7:
. 表示匹配任意單個字符
[root@vm1 test]# grep -n "b." text.txt 2:I like badminton, snooker, running 4:Maybe I‘m not a smart person, but I‘m working hard. 5:My blog is http://www.cnblogs.com/Caiyundo/ 6:Welcome to my blog! CAIYUN
\ 表示轉義符
[root@vm1 test]# grep -n "\.$" text.txt 1:My name is Caiyun. 4:Maybe I‘m not a smart person, but I‘m working hard. 9:My mail is [email protected].
* 表示重復前面0個或1個以上字符
[root@vm1 test]# grep -n "bl*" text.txt 2:I like badminton, snooker, running 4:Maybe I‘m not a smart person, but I‘m working hard. 5:My blog is http://www.cnblogs.com/Caiyundo/ 6:Welcome to my blog! CAIYUN
.* 表示匹配所有
[root@vm1 test]# grep -n ".*" text.txt 1:My name is Caiyun. 2:I like badminton, snooker, running 3: 4:Maybe I‘m not a smart person, but I‘m working hard. 5:My blog is http://www.cnblogs.com/Caiyundo/ 6:Welcome to my blog! CAIYUN 7: 8:My qq is 791111890 9:My mail is [email protected].
[] 表示匹配"[]"裏面任意單個字符
[root@vm1 test]# grep -n "[791]" text.txt 8:My qq is 791111890 9:My mail is Caiyun111111@gmail.com.
[^] 表示取反"[^]"裏面的任意單個字符
[root@vm1 test]# echo Caiyun >> test2.txt [root@vm1 test]# echo 791111890 >> test2.txt [root@vm1 test]# grep -n "[^0-9]" test2.txt 1:Caiyun
[-] 表示匹配"[-]"裏一段字符的任意單個字符,如[0-9]即0到9
[root@vm1 test]# grep -n "[0-9]" text.txt 8:My qq is 791111890 9:My mail is Caiyun111111@gmail.com.
1\{4\} 表示匹配字符 "1" 重復4次
[root@vm1 test]# grep -n "1\{4\}" text.txt 8:My qq is 791111890 9:My mail is Caiyun1111[email protected].
1\{5,\} 表示匹配字符 "1" 重復5次或5次以上
[root@vm1 test]# grep -n "1\{5,\}" text.txt 9:My mail is Caiyun111111@gmail.com.
1\{,6\} 表示匹配字符 "1" 重復6次和6次以內
[root@vm1 test]# grep -n "1\{,6\}" text.txt 1:My name is Caiyun. 2:I like badminton, snooker, running 3: 4:Maybe I‘m not a smart person, but I‘m working hard. 5:My blog is http://www.cnblogs.com/Caiyundo/ 6:Welcome to my blog! CAIYUN 7: 8:My qq is 791111890 9:My mail is Caiyun111111@gmail.com.
1\{3,5\} 表示匹配字符 "1" 重復3-5次
[root@vm1 test]# grep -n "1\{3,5\}" text.txt 8:My qq is 791111890 9:My mail is Caiyun11111[email protected].
二、擴展正則表達式
+ 表示重復 "一個或一個以上" 前面的所有字符("*"是0個)
[root@vm1 test]# grep -n "11111*" text.txt 8:My qq is 791111890 9:My mail is Caiyun111111@gmail.com. [root@vm1 test]# grep -n "11111\+" text.txt 9:My mail is Caiyun111111@gmail.com.
? 表示重復 "0個或一個" 前面的字符("."是有且只有1個)
[root@vm1 test]# grep -n "11111." text.txt 9:My mail is Caiyun111111@gmail.com. [root@vm1 test]# grep -n "11111\?" text.txt 8:My qq is 791111890 9:My mail is Caiyun11111[email protected].
| 表示過濾多個字符串
[root@vm1 test]# grep -nE "CAIYUN|mail" text.txt 6:Welcome to my blog! CAIYUN 9:My mail is Caiyun111111@gmail.com. [root@vm1 test]# grep -n "CAIYUN\|mai" text.txt 6:Welcome to my blog! CAIYUN 9:My mail is Caiyun111111@gmail.com.
一般情況下,當我們需要用到擴展正則表達式匹配符時,使用grep要加參數 ”-E” 或用 “\” 轉譯符轉義匹配符
三、元字符
\b 邊界字符,單詞邊界
[root@vm1 test]# grep ‘Caiyun‘ text.txt My name is Caiyun. My blog is http://www.cnblogs.com/Caiyundo/ My mail is Caiyun[email protected]. [root@vm1 test]# grep ‘Caiyun\b‘ text.txt My name is Caiyun.
grep的用法還有很多,只要符合規則、邏輯,還可以和很多其他命令配合使用。
PS:要活學活用,多用多練。
Linux三劍客之老三grep