4 grep命令
1、文本搜索工具
2、GREP 是Global search Regular Expression and Print out the line的簡(jiǎn)稱(chēng),即全面搜索正則表達(dá)式并把行打印出來(lái)。
3、grep命令的模式十分靈活,可以是字符串,也可以是變量,還可以是正則表達(dá)式。模式中包含空格,則必須用雙引號(hào)括起來(lái)。
4.1 基本用法
選 項(xiàng) | 意 義 |
---|---|
-c | 只輸出匹配行的數(shù)量 |
-i | 搜索時(shí)忽略大小寫(xiě) |
-h | 查詢(xún)多文件時(shí)不顯示文件名 |
-I | 只列出符合匹配的文件名,而不列出具體的匹配行 |
-n | 列出所有的匹配行,并顯示行號(hào) |
-s | 不顯示不存在或無(wú)匹配文本的錯(cuò)誤信息 |
-v | 顯示不包含匹配文本的所有行 |
-w | 匹配整詞 |
-x | 匹配整行 |
-r | 遞歸搜索,不僅搜索當(dāng)前工作目錄,而且搜索子目錄 |
-q | 禁止輸出任何結(jié)果,以退出狀態(tài)表示搜索是否成功 |
-b | 打印匹配行距文件頭部的偏移量,以字節(jié)為單位 |
-0 | 與-b選項(xiàng)結(jié)合使用,打印匹配的詞距文件頭部的偏移量,以字節(jié)為單位 |
-E | 支持?jǐn)U展的正則表達(dá)式 |
-F | 不支持正則表達(dá)式,按照字符串的字面意思進(jìn)行匹配 |
4.2 參考命令
命令模式指的是將命令封裝成一個(gè)整體。
4.2.1 雙引號(hào)
// 含空格時(shí)使用雙引號(hào)
[rhel@localhost ~]$ grep -i "user for" /etc/passwd
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
geoclue:x:994:991:User for geoclue:/var/lib/geoclue:/sbin/nologin
4.2.2 -c 輸出匹配行數(shù)
// -c輸出匹配的行數(shù)
[rhel@localhost ~]$ grep -c -i "user for" /etc/passwd
3
// -n 列出匹配項(xiàng)并加上行號(hào)。
[rhel@localhost ~]$ grep -n -i "user for" /etc/passwd
16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
19:colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
27:geoclue:x:994:991:User for geoclue:/var/lib/geoclue:/sbin/nologin
4.2.3 -h 或 -l 不顯示或只顯示文件名
// -h 顯示文件名 -l 只列出符合規(guī)范的文件名
[rhel@localhost etc]$ grep -i home passwd*
passwd:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
passwd:db2inst1:x:1001:901::/home/db2inst1:/bin/bash
passwd:db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
passwd:db2as:x:1003:903::/home/db2as:/bin/bash
passwd-:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
passwd-:db2inst1:x:1001:901::/home/db2inst1:/bin/bash
passwd-:db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
[rhel@localhost etc]$ grep -h home passwd*
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
db2inst1:x:1001:901::/home/db2inst1:/bin/bash
db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
db2as:x:1003:903::/home/db2as:/bin/bash
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
db2inst1:x:1001:901::/home/db2inst1:/bin/bash
db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
[rhel@localhost etc]$ grep -l home passwd*
passwd
passwd-
4.2.4 -s 不顯示錯(cuò)誤信息
[rhel@localhost etc]$ grep user else passwd
grep: else: 沒(méi)有那個(gè)文件或目錄
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd:radvd:x:75:75:radvd user:/:/sbin/nologin
passwd:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
passwd:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
passwd:qemu:x:107:107:qemu user:/:/sbin/nologin
passwd:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
[rhel@localhost etc]$ grep -s user else passwd
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd:radvd:x:75:75:radvd user:/:/sbin/nologin
passwd:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
passwd:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
passwd:qemu:x:107:107:qemu user:/:/sbin/nologin
passwd:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
4.2.5 -r 遞歸顯示本級(jí)目錄及下級(jí)目錄
[root@localhost etc]# grep auth pa*
grep: pam.d: 是一個(gè)目錄
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd-:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
[root@localhost etc]# grep -s auth pa*
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd-:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
[root@localhost etc]# grep -r auth pa*
pam.d/config-util:auth sufficient pam_rootok.so
pam.d/config-util:auth sufficient pam_timestamp.so
pam.d/config-util:auth include system-auth
pam.d/config-util:session optional pam_xauth.so
4.2.6 -w 匹配完整詞 -x 匹配完整行
[root@localhost etc]# grep tcpd* passwd
tcpdump:x:72:72::/:/sbin/nologin
[root@localhost etc]# grep -w tcpd* passwd
[root@localhost etc]#
[root@localhost etc]# grep tcpd* passwd
tcpdump:x:72:72::/:/sbin/nologin
[root@localhost etc]# grep -x tcpdump passwd
[root@localhost etc]# grep -x tcpdump:x:72:72::/:/sbin/nologin passwd
tcpdump:x:72:72::/:/sbin/nologin
4.2.7 -q 退出狀態(tài)
grep命令后一旦加上-q選項(xiàng), grep將不再輸出任何結(jié)果,而是以退出狀態(tài)表示搜索是否成功,退出狀態(tài)0表示搜索成功,退出狀態(tài)1表示未搜索到滿(mǎn)足模式的文本行,退出狀態(tài)2表示命令或程序由于錯(cuò)誤而未能執(zhí)行。
[root@localhost etc]# grep -q -x tcpdump:x:72:72::/:/sbin/nologin passwd
[root@localhost etc]# echo $?
0 --退出狀態(tài)0
[root@localhost etc]# grep -q -x tcpdump passwd
[root@localhost etc]# echo $?
1 --退出狀態(tài)1
[root@localhost etc]# grep -q -s -x tcpdump passwdmmm
[root@localhost etc]# echo $?
2 --退出狀態(tài)2
4.2.8 -b 距文件頭部的偏移量
grep-b 選項(xiàng)打印匹配行
距文件頭部的偏移量,以字節(jié)為單位。
如果在-b 選項(xiàng)后再加上-o 選項(xiàng) ,grep命令將打印匹配的詞
距文件頭部的偏移量。
[root@localhost etc]# grep -b bash passwd
0:root:x:0:0:root:/root:/bin/bash
2016:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
2059:db2inst1:x:1001:901::/home/db2inst1:/bin/bash
2105:db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
2151:db2as:x:1003:903::/home/db2as:/bin/bash
[root@localhost etc]# grep -b -o bash passwd
27:bash
2054:bash
2100:bash
2146:bash
2186:bash
[root@localhost etc]#
4.2.9 -E -F
grep命令的-E 和-F 選項(xiàng)分別等價(jià)于grep命令族中的egrep和 fgrep命令
4.3 grep與正則表達(dá)式
4.3.1 匹配行首
// 匹配以n開(kāi)頭的行首
[root@localhost etc]# grep ^n passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@localhost etc]#
4.3.2 查找空白行(^)
// 統(tǒng)計(jì)空白行的數(shù)量
[root@localhost etc]# grep -c ^$ profile
11
// 統(tǒng)計(jì)非空白行行數(shù)
[root@localhost etc]# grep -c ^[^$] profile
65
4.3.3 設(shè)置大小寫(xiě)(-n)
[root@localhost etc]# grep -i RHEL passwd
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
[root@localhost etc]# grep -n [Rr]hel passwd
40:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
4.3.4 轉(zhuǎn)義符(\)
[root@localhost etc]# grep cj\.name makedumpfile.conf.sample
## erase cj.name
[root@localhost rhel]# grep '\-\{5\}' 1.sh
-----dddfsdf
-----sdfsdfasdfaew
4.3.5 POSIX字符
為了保持不同國(guó)家的字符編碼的一致性, POSIX(Portable Operating System Interface)增 加了特殊的字符類(lèi),以[:classname]的格式給出, grep 命令支持POSIX 字符類(lèi)
類(lèi) 名 | 意 義 |
---|---|
[:upper:] | 表示大寫(xiě)字母[A~Z] |
[:lower:] | 表示小寫(xiě)字母[a~z] |
[:digit:] | 表示阿拉伯?dāng)?shù)字[0~9] |
[:alnum:] | 表示大小寫(xiě)字母和阿拉伯?dāng)?shù)字[0~9a~zA~Z] |
[:space:] | 表示空格或Tab鍵 |
[:alpha:] | 表示大小寫(xiě)字母[a~zA~Z] |
[:cntrl:] | 表示Ctrl鍵 |
[:graph:]或[:print:] | 表示ASCIⅡI碼33~126之間的字符 |
[:xdigit:] | 表示16進(jìn)制數(shù)字[0~9 A~Fa~f] |
//利用POSIX 字符類(lèi)搜索以大寫(xiě)字母開(kāi)頭的行
[root@zawu globus]# grep ^[[:upper:]] 00.pem
This is a Certificate Request file:
It should be mailed to xd nidseu edu cn
Certificate Subject: _ . .
The above string is known as your user certificate subject, and it To install this user certificate, please save this e-mail message If you have any questions about the certificate contactMIIB4ZCCAUwCAQAwcTENMAsGA1UEChMER3JpZDETMBEGA1UECxMKR2xvYnVzVGCxMKc2V1LmVkdS5jbjEPMAOGA1UEAxMGZ2XvYnVZMIGfMAOGCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCw50H88r7sAGjQGLZTmMxTiw9AgDqppBMhP6Fg3eJTqBvqBdhaYTRtleSBT/AJUi3rTDRIABJPgu8cZkwb1AE8uEJSeCKwgk3J9QHK2NcZXwIDAQABBAIWADANBgkqhkiG9wOBAQQFAAOBgQBoHRUaaB/Tyu+LuALwnT3Muw/OjDIYxc5aYaA4dwCB6/2yVYyfmyRCNox3rIsyUvqL9p81d/hpNiAB/0OazMBialq5Gcpaansd
[rootezawu globus]#
//搜索以空格開(kāi)頭文本行
[rootazawu globus]# grep ^[[:space:]] 00.pem
/0=Grid/OU=GlobusTest/OU=simpleCA-seugrid1 .seu .edu .cn/OU=seu .edu .cn/CN=globus
You need not edit this message in any way . Simply
save this e-mail message to the file.
4.3.6 精確匹配
[root@localhost rhel]# grep the 1.sh
Linel:there are four lines in this file
Line2:this the line 2
Line3:this is another line
[root@localhost rhel]# grep "\<the\>" 1.sh
Line2:this the line 2
[root@localhost rhel]# grep -w the 1.sh
Line2:this the line 2
4.3.7 或字符
字符“ | ”是擴(kuò)展的正則表達(dá)式中定義的, grep需要加上-E 選項(xiàng)才能支持它,下面給出grep命令使用“ | ”字符的例子。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-600920.html
[root@localhost rhel]# grep -E "there | another " 1.sh
Linel:there are four lines in this file
Line3:this is another line
4.4 grep 命令族簡(jiǎn)介
Linux 系統(tǒng)支持三種形式的grep命令,通常將這三種形式的 grep命令稱(chēng)為 grep命令族, 這三種形式具體為:
● grep: 標(biāo)準(zhǔn)grep 命令,支持基本正則表達(dá)式,上面兩小節(jié)已經(jīng)對(duì)此命令進(jìn)行了詳細(xì) 的討論。
● egrep: 擴(kuò)展grep 命令,支持基本和擴(kuò)展正則表達(dá)式。
● fgrep: 快速grep 命令,不支持正則表達(dá)式,按照字符串的字面意思進(jìn)行匹配。
egrep 命令與 grep -E 等價(jià), fgrep 命令與 grep -F 等價(jià),在某些Linux 發(fā)行版中, egrep 和fgrep都是grep命令的別名,分別將其符號(hào)鏈接到grep-E 和 grep-F 命令。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-600920.html
[root@localhost rhel]# egrep "there | another " 1.sh
Linel:there are four lines in this file
Line3:this is another line
[root@localhost rhel]# fgrep another 1.sh
Line3:this is another line
到了這里,關(guān)于Linux Shell 腳本編程學(xué)習(xí)之【第3章 正則表達(dá)式 (第二部分) grep命令】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!