1、grep:用于查找文件里符合條件的字符串(內(nèi)容)。豐富的參數(shù)用于對搜索過程的補(bǔ)充。命令模式十分靈活,可以是變量、字符串、正則表達(dá)式。
2、linux 支持 grep、egrep 和 fgrep。grep 和 egrep 都支持正則表達(dá)式,只不過 egrep 支持的是擴(kuò)展正則表達(dá)式。fgrep 不支持正則表達(dá)式,只支持普通字符串的過濾。
3、grep 加上相應(yīng)的參數(shù)可以實(shí)現(xiàn) egrep 和 fgrep 的功能。所以,也可以用 grep 加上對應(yīng)的參數(shù)來執(zhí)行 egrep 和 fgrep。
4、通過 man grep 查看說明。

- 命令格式上的 PATTERN (模式)相當(dāng)于過濾條件,過濾文件中條件匹配的行。

- grep 通過 -E 參數(shù)切換到 egrep 和 -F 參數(shù)切換到 fgrep 的功能。

5、grep 命令的基本操作:
- ①、grep 過濾標(biāo)準(zhǔn)輸入。
-
grep abc:回車之后,系統(tǒng)等待標(biāo)準(zhǔn)輸入。abc 是過濾條件,等待后續(xù)輸入內(nèi)容中是否含有 abc。
等待標(biāo)準(zhǔn)輸入

-
也可以用輸入重定向到標(biāo)準(zhǔn)輸入,輸入的內(nèi)容匹配過濾條件。end 為結(jié)束符。
輸入重定向 綜上測試,grep 支持標(biāo)準(zhǔn)輸入。也就是可以使用管道,管道右邊是需要有輸入功能的。 grep 更多情況下是根據(jù)文件進(jìn)行操作,很多場景是配合管道執(zhí)行。
②、grep 根據(jù)條件過濾文件內(nèi)容。
- 過濾 /etc/passwd 的 root 。只要 /etc/passwd 含有 root 的行都會列出來。

- grep 可以接受變量,如上圖寫死了 root 作為 grep 的過濾條件,也可以用變量形式作為過濾條件。
[root@localhost ~]# echo $USER ## USER 是一個變量,表示當(dāng)前的用戶名
root
[root@localhost ~]#
[root@localhost ~]# USER=torres ## USER 賦值為 torres
[root@localhost ~]#
[root@localhost ~]# echo $USER ## USER 當(dāng)前值是 torres
torres
## grep 在 /etc/passwd 目錄過略 USER 變量的值
[root@localhost ~]# grep $USER /etc/passwd
torres:x:1000:1007::/home/torres:/bin/bash ## 含有 torres 的行也會被列出來
[root@localhost ~]#
- grep 可以調(diào)用其他命令的結(jié)果,如 whoami 本身是個命令,查詢當(dāng)前的用戶。被調(diào)用的命令要用反單引號括起來。grep 調(diào)用 whoami 的結(jié)果,那么 whoami 要用反單引號括起來。
[root@localhost ~]#
[root@localhost ~]# whoami ## whoami 是個命令,查詢當(dāng)前的用戶
root
[root@localhost ~]# grep `whoami` /etc/passwd ## `whoami`的結(jié)果會被 grep 調(diào)用
root:x:0:0:root:/root:/bin/bash ## /etc/passwd 含有 root 的行都會列出來
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#
三、grep 命令的常用參數(shù)。
①、--color=auto:對匹配的字符串用高光顯示。
②、-v:排除過濾條件的行,也就是顯示排除條件以外的內(nèi)容。
③、-i:忽略大小寫。
④、-n:顯示匹配的行號,列出內(nèi)容的同時也列出行號。
⑤、-c:統(tǒng)計(jì)匹配的行數(shù),只列出共多少行符合過略條件,不列出內(nèi)容。
⑥、-o:僅顯示匹配的字符串,只顯示文本中含過濾條件的字符串。
⑦、-q:靜默模式,不輸出任何信息。用于返回值判斷,不考慮輸出內(nèi)容。
⑧、-A:(after),顯示包含當(dāng)前字符串的后多少行。
⑨、-B:(before),顯示包含當(dāng)前字符串的前多少行。
⑩、-C:(context),顯示包含當(dāng)前字符串的前后多少行。
⑩-①、-e:or,或。用于多個參數(shù)間的邏輯 或 判斷。
⑩-②、-w:精確匹配,匹配整個單詞。
⑩-③、-f:把過濾條件放到文件中,通過讀取文件的過濾條件進(jìn)行過濾。
⑩-④、-E:使用 egrep。(支持?jǐn)U展正則表達(dá)式)
⑩-⑤、-F:使用 fgrep(不支持正則表達(dá)式)。
- ①、--color=auto:對匹配的字符串用高光顯示。
- grep 過濾條件匹配的內(nèi)容會有標(biāo)紅的功能。CentOS7 系統(tǒng)執(zhí)行 grep 的時候是執(zhí)行別名。它加了一個 --color=auto 參數(shù)才支持標(biāo)紅。
[root@localhost ~]# alias grep ## 列出 grep 別名
alias grep='grep --color=auto'
[root@localhost ~]#
- 如 grep 過濾出來的內(nèi)容沒有標(biāo)紅可以參考以下方法:
[root@localhost ~]# vim .bashrc ## 編輯 root 家目錄下的 .bashrc 文件
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias grep='grep --color=auto' ## <---- 添加別名并輸入 --color=auto 參數(shù)
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
~
~
~
:wq ## 保存退出
[root@localhost ~]# source ~/.bashrc ## .bashrc 文件生效
[root@localhost ~]#
②、-v:排除過濾條件的行,也就是顯示排除條件以外的內(nèi)容。
- 顯示 /etc/passwd 排除了 sbin 字符串的行。
[root@localhost ~]# grep -v "sbin" /etc/passwd
root:x:0:0:root:/root:/bin/bash
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]#
[root@localhost ~]#
③、-i:忽略大小寫。
## linux 大小寫敏感,“Root” 和 “root” 是不一樣的字符串?!癛oot” 不會有顯示
[root@localhost ~]# grep "Root" /etc/passwd
[root@localhost ~]#
## -i 可以忽略大小寫,只要是 root 這 4個字母都可以搜索出來
[root@localhost ~]# grep -i "Root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#
④、-n:顯示匹配的行號,列出內(nèi)容的同時也列出行號。
[root@localhost ~]#
[root@localhost ~]# grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash ## 列出行號
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#
## grep -n 和 cat -n 效果相似。(cat -n 輸出結(jié)果后,管道 grep 過略 root 這個字符串)
[root@localhost ~]# cat -n /etc/passwd | grep "root"
1 root:x:0:0:root:/root:/bin/bash
10 operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#
⑤、-c:統(tǒng)計(jì)匹配的行數(shù),只列出共多少行符合過略條件,不列出內(nèi)容。
[root@localhost ~]#
[root@localhost ~]# grep -c "root" /etc/passwd ## 含有 root 字符串的有 2 行
2
## grep -c 等價于 wc -l
[root@localhost ~]# grep "root" /etc/passwd | wc -l
2
[root@localhost ~]#
⑥、-o:僅顯示匹配的字符串,只顯示文本中含過濾條件的字符串。
- 文本中出現(xiàn)多少次 root,就會打印多少次。
[root@localhost ~]# grep -o "root" /etc/passwd
root
root
root
root
[root@localhost ~]#
⑦、-q:靜默模式,文本中包不包含過濾條件的字符串都不輸出任何信息。用于返回值判斷,命令執(zhí)行成功返回 0,失敗返回非 0。
## /etc/passwd 有 root 字符串,不會輸出任何信息。
[root@localhost ~]# grep -q "root" /etc/passwd
[root@localhost ~]#
[root@localhost ~]# echo $? ## 通過打印 $? 返回值是 0,命令執(zhí)行成功,也就是包含 root
0
## /etc/passwd 沒有 bbbbbb 字符串,也不會輸出任何信息。
[root@localhost ~]# grep -q "bbbbbb" /etc/passwd
[root@localhost ~]#
[root@localhost ~]# echo $? ## 通過打印 $? 返回值非 0,命令執(zhí)行失敗,也就是沒有包含 bbbbbb
1
[root@localhost ~]#
- -q 靜默模式等價于輸出重定向的 &> /dev/null(標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤輸出重定向到垃圾桶),使用于返回值作判斷。
## grep 過濾 /etc/passwd 文件中的 root 字符串,正確輸出和錯誤輸出都重定向到垃圾桶
## 執(zhí)行這樣的命令沒有信息返回,只有執(zhí)行成功與否的返回值。
[root@localhost ~]# grep "root" /etc/passwd &> /dev/null
[root@localhost ~]#
[root@localhost ~]# echo $? ## /etc/passwd 中有 root 返回 0
0
[root@localhost ~]#
[root@localhost ~]# grep "zzzzzz" /etc/passwd &> /dev/null
[root@localhost ~]#
[root@localhost ~]# echo $? ## /etc/passwd 中沒有 zzzzzz 返回非 0
1
[root@localhost ~]#
- 需要注意的是失敗返回非0,不一定是 1。
[root@localhost ~]#
[root@localhost ~]# ls cron.txt &> /dev/null
[root@localhost ~]#
[root@localhost ~]# echo $? ## 當(dāng)前目錄有 cron.txt,返回 0
0
[root@localhost ~]#
[root@localhost ~]# ls abc.txt &> /dev/null
[root@localhost ~]#
[root@localhost ~]# echo $? ## 當(dāng)前目錄沒有 abc.txt,返回 2(非 0)
2
[root@localhost ~]#
⑧、-A:(after),顯示包含當(dāng)前字符串的后多少行。
[root@localhost ~]# grep -nA3 "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash ## 含 root 的第 1 行
2-bin:x:1:1:bin:/bin:/sbin/nologin ## 第 1 行后的 3 行顯示出來
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
4-adm:x:3:4:adm:/var/adm:/sbin/nologin
--
10:operator:x:11:0:operator:/root:/sbin/nologin ## 含 root 的第 10 行
11-games:x:12:100:games:/usr/games:/sbin/nologin ## 第 10 行后的 3 行顯示出來
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:99:99:Nobody:/:/sbin/nologin
[root@localhost ~]#
⑨、-B:(before),顯示包含當(dāng)前字符串的前多少行。
[root@localhost ~]#
[root@localhost ~]# grep -nB3 "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash ## 當(dāng)前第 1 行含 root,前面沒有行可以顯示
--
7-shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown ## 第 10 行前 3 行
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin ## 第 10 行含 root
[root@localhost ~]#
⑩、-C:(context),顯示包含當(dāng)前字符串的前后多少行。
[root@localhost ~]#
[root@localhost ~]# grep -nC3 "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash ## 第 1 行含有 root,只能顯示后 3 行
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
--
7-shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown ## 第 10 行的前 3 行顯示出來
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin ## 第 10 行含有 root
11-games:x:12:100:games:/usr/games:/sbin/nologin ## 第 10 行的后 3 行顯示出來
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:99:99:Nobody:/:/sbin/nologin
[root@localhost ~]#
⑩-①、-e:or,或。用于多個參數(shù)間的邏輯 或 判斷。
- 過濾 /etc/passwd 文件下含有 root 或者 bash 字符串的行。
[root@localhost ~]#
[root@localhost ~]# grep -e "root" -e "bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash ## 含有 root 或 bash
operator:x:11:0:operator:/root:/sbin/nologin ## 含有 root
torres:x:1000:1007::/home/torres:/bin/bash ## 含有 bash
user1:x:1001:1008::/home/user1:/bin/bash ## 含有 bash
x:x:1002:1009::/home/x:/bin/bash ## 含有 bash
[root@localhost ~]#
- 多重條件過略,只需多重 -e 跟過略條件即可,過濾 /etc/passwd 文件下含有 root 或者 bash 或者 nologin 字符串的行。
[root@localhost ~]# grep -e "root" -e "bash" -e "nologin" /etc/passwd
root:x:0:0:root:/root:/bin/bash ## 含有 root 或 bash
bin:x:1:1:bin:/bin:/sbin/nologin ## 含有 nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin ## 含有 nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin ## 含有 nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin ## 含有 nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin ## 含有 nologin
operator:x:11:0:operator:/root:/sbin/nologin ## 含有 root 或 nologin
games:x:12:100:games:/usr/games:/sbin/nologin ## 含有 nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin ## 含有 nologin
nobody:x:99:99:Nobody:/:/sbin/nologin ## 含有 nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin ## 含有 nologin
dbus:x:81:81:System message bus:/:/sbin/nologin ## 含有 nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin ## 含有 nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin ## 含有 nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin ## 含有 nologin
torres:x:1000:1007::/home/torres:/bin/bash ## 含有 bash
user1:x:1001:1008::/home/user1:/bin/bash ## 含有 bash
x:x:1002:1009::/home/x:/bin/bash ## 含有 bash
[root@localhost ~]#
- 多重過略條件并存,可用管道進(jìn)行過濾。過濾 /etc/passwd 文件中含既有 root 也有 bash 字符串的行。
[root@localhost ~]# grep "root" /etc/passwd | grep "bash"
root:x:0:0:root:/root:/bin/bash
⑩-②、-w:精確匹配,匹配整個單詞。
- grep "bin",語句的意思是包含 bin 的都過濾出來。會過濾很多 bin,sbin 的數(shù)據(jù)出來,因?yàn)?sbin 包含 bin。
[root@localhost ~]#
[root@localhost ~]# grep "bin" /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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
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
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]#
- 而加上 -w 參數(shù)的 grep -w "bin" ,就可以顯示完全匹配單詞的那些行。
[root@localhost ~]#
[root@localhost ~]# grep -w "bin" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]#
- -w 等價于 \b\b、\b 是邊界的意思。兩個 \b 括住的 bin 就是精確過濾的條件。
[root@localhost ~]# grep "\bbin\b" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]#
- -w 也等價于 <>, <>也是邊界的意思。< 內(nèi)容 > 是精確過濾的條件。
[root@localhost ~]# grep "\<bin\>" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]#
- grep 可以識別單詞的分隔符:
[root@localhost ~]# echo "a abc b" | grep -w "abc"
a abc b
[root@localhost ~]# echo "a-abc-b" | grep -w "abc"
a-abc-b
[root@localhost ~]# echo "a,abc,b" | grep -w "abc"
a,abc,b
[root@localhost ~]# echo "a/abc/b" | grep -w "abc"
a/abc/b
[root@localhost ~]# echo "a\abc\b" | grep -w "abc"
a\abc\b
[root@localhost ~]# echo "a'abc'b" | grep -w "abc"
a'abc'b
[root@localhost ~]# echo "a~abc~b" | grep -w "abc"
a~abc~b
[root@localhost ~]# echo "a@abc@b" | grep -w "abc"
a@abc@b
[root@localhost ~]# echo "a%abc%b" | grep -w "abc"
a%abc%b
[root@localhost ~]# echo "a*abc*b" | grep -w "abc"
a*abc*b
[root@localhost ~]# echo "a(abc)b" | grep -w "abc"
a(abc)b
[root@localhost ~]# echo "a+abc+b" | grep -w "abc"
a+abc+b
[root@localhost ~]# echo "a>abc>b" | grep -w "abc"
a>abc>b
[root@localhost ~]# echo "a<abc<b" | grep -w "abc"
a<abc<b
[root@localhost ~]# echo "a>abc<b" | grep -w "abc"
a>abc<b
[root@localhost ~]# echo "a?abc?b" | grep -w "abc"
a?abc?b
- grep 不能識別單詞的分隔符:
[root@localhost ~]# echo "a_abc_b" | grep -w "abc"
[root@localhost ~]# echo $? ## 返回值非 0 ,命令執(zhí)行失敗
1
[root@localhost ~]# echo "a1abc2b" | grep -w "abc"
[root@localhost ~]# echo $?
1
[root@localhost ~]# echo "a$abc$b" | grep -w "abc"
[root@localhost ~]# echo $?
1
[root@localhost ~]# echo "a`abc`b" | grep -w "abc"
-bash: abc: command not found
[root@localhost ~]#
⑩-③、-f:把過濾條件放到文件中,通過讀取文件的過濾條件進(jìn)行過濾。
[root@localhost ~]#
[root@localhost ~]# vim grepTest.txt ## 編輯 grepTest.txt,存儲過濾條件
root
bash
~
~
~
:wq
[root@localhost ~]#
[root@localhost ~]# grep -f grepTest.txt /etc/passwd ## -f 讀取 grepTest.txt 過濾條件
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]#

