Linux grep命令介紹
grep
(Global Regular Expression Print)命令用來(lái)在文件中查找包含或者不包含某個(gè)字符串的行,它是強(qiáng)大的文本搜索工具,并可以使用正則表達(dá)式進(jìn)行搜索。當(dāng)你需要在文件或者多個(gè)文件中搜尋特定信息時(shí),grep就顯得無(wú)比重要啦。
Linux grep命令適用的Linux版本
grep命令在幾乎所有的Linux發(fā)行版中都可以使用。以下是在CentOS 7和CentOS 8中安裝grep的命令。
[linux@bashcommandnotfound.cn ~]$ sudo yum install grep # for CentOS 7
[linux@bashcommandnotfound.cn ~]$ sudo dnf install grep # for CentOS 8
Linux grep命令的基本語(yǔ)法
語(yǔ)法格式:
grep [options] pattern [file...]
Linux grep命令的常用選項(xiàng)或參數(shù)說(shuō)明
參數(shù) | 說(shuō)明 |
---|---|
-v | –invert-match 反向選擇,只顯示沒(méi)有匹配到的行 |
-i | –ignore-case 忽略大小寫(xiě) |
-r | –recursive 遞歸處理,指定目錄下的所有文件以及子目錄中的文件 |
-l | –files-with-matches 列出文件內(nèi)容符合指定的樣式的文件名稱(chēng) |
-n | –line-number 顯示匹配行及其行號(hào) |
–color=auto | –color 在顯示匹配行時(shí),將匹配的字符串以特定顏色突出顯示 |
Linux grep命令實(shí)例詳解
實(shí)例1:使用grep查找包含特定字符串的行
使用grep,我們可以在文件中查找包含特定字符串的行。這是grep的基本用法。
[linux@bashcommandnotfound.cn ~]$ grep 'pattern' filename
實(shí)例2:使用grep和正則表達(dá)式查找字符串
grep不僅能夠基于字符串搜尋信息,還能夠搭配正則表達(dá)式進(jìn)行更為復(fù)雜的搜索。
[linux@bashcommandnotfound.cn ~]$ grep 'regex' filename
實(shí)例3:使用grep在多個(gè)文件中搜索
grep Command 不僅可以在一個(gè)文件中進(jìn)行搜索,也可以在多個(gè)文件中查找匹配的行。
[linux@bashcommandnotfound.cn ~]$ grep 'pattern' file1 file2 file3
實(shí)例4:使用grep配合通配符搜索
在某種情況下,你可能需要在特定類(lèi)型的文件,如所有的文本(.txt)文件中進(jìn)行搜索,可以使用通配符(*)。
[linux@bashcommandnotfound.cn ~]$ grep 'pattern' *.txt
實(shí)例5:使用grep查找不符合匹配的行
如果你想查找不包含某些字符串或者模式的行,可以使用 -v 選項(xiàng)。
[linux@bashcommandnotfound.cn ~]$ grep -v 'pattern' filename
實(shí)例6:使用grep搜索并高亮匹配內(nèi)容
使用 --color=auto 選項(xiàng),可以高亮顯示匹配的字符串。
[linux@bashcommandnotfound.cn ~]$ grep --color=auto 'pattern' filename
實(shí)例7:使用grep讀取另一個(gè)命令的輸出
grep命令可以配合管道操作符(|)搜尋另一個(gè)命令的輸出。
[linux@bashcommandnotfound.cn ~]$ command | grep 'pattern'
實(shí)例8:使用grep顯示匹配字符串的前后行
-c選擇項(xiàng),它除了可以列出行號(hào)外,還可以列出符合范本樣式的具體是哪些行,假設(shè)我們希望找出符合范本樣式的前2行,那么我們可以這樣寫(xiě):
[linux@bashcommandnotfound.cn ~]$ grep -B 2 'pattern' filename
實(shí)例9:在文件中搜索多個(gè)模式
你可以在同一文件中查找多個(gè)模式。只需要使用-e選項(xiàng)就可以達(dá)到這個(gè)目的。
[linux@bashcommandnotfound.cn ~]$ grep -e 'pattern1' -e 'pattern2' filename
實(shí)例10:在文本中查找數(shù)字
有的時(shí)候你可能需要基于握手的數(shù)字范圍來(lái)進(jìn)行搜索。我們可以結(jié)合正則表達(dá)式來(lái)進(jìn)行搜索。
[linux@bashcommandnotfound.cn ~]$ grep '[0-9]' filename
實(shí)例11:在一個(gè)目錄中查找含有某一字符串的文件
grep指令可以在一個(gè)目錄中的所有文件中搜尋含有某一指定字符串的文件。
[linux@bashcommandnotfound.cn ~]$ grep -r 'pattern' directory
實(shí)例12:統(tǒng)計(jì)文件中匹配某個(gè)字符串的行數(shù)
使用grep -c我們可以輕易得到文件中匹配特定字符串的行數(shù)。
[linux@bashcommandnotfound.cn ~]$ grep -c 'pattern' filename
實(shí)例13:查找特定格式的字符串
有時(shí),我們可能需要查找符合特定格式的字符串,如,我們可以找出所有格式為字母-字母-字母的字符串。
[linux@bashcommandnotfound.cn ~]$ grep '[A-Za-z]-[A-Za-z]-[A-Za-z]' filename
實(shí)例14:使用grep且忽略大小寫(xiě)
有時(shí)候我們對(duì)大小寫(xiě)并不敏感,可以通過(guò) -i 選項(xiàng)忽略大小寫(xiě)進(jìn)行查找:
[linux@bashcommandnotfound.cn ~]$ grep -i 'pattern' filename
實(shí)例15:在多級(jí)目錄中使用grep搜索
使用 -R 或 -r 選項(xiàng),grep 命令可以在多級(jí)子目錄中進(jìn)行遞歸搜索:
[linux@bashcommandnotfound.cn ~]$ grep -R 'pattern' directory
實(shí)例16:顯示匹配結(jié)果的上下文
有時(shí)候我們想知道匹配行的上下文信息,即查看它前后的行??梢允褂?-A,-B,-C 選項(xiàng)完成這個(gè)需求:
[linux@bashcommandnotfound.cn ~]$ grep -C 5 'pattern' filename #-A 5顯示匹配行之后5行,-B 5顯示匹配行之前5行
實(shí)例17:顯示包含匹配行的文件名
如果你想知道包含匹配行的文件名,可以使用 -l 選項(xiàng):
[linux@bashcommandnotfound.cn ~]$ grep -l 'pattern' file1 file2 file3
實(shí)例18:使用egrep完成多模式搜索
egrep 是 grep 的拓展版,它可以同時(shí)進(jìn)行多模式搜索:
[linux@bashcommandnotfound.cn ~]$ egrep 'pattern1|pattern2' filename
實(shí)例19:grep中的正則表達(dá)式
grep可以配合正則表達(dá)式來(lái)使用,非常靈活和強(qiáng)大:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-799781.html
[linux@bashcommandnotfound.cn ~]$ grep '^pattern' filename #搜索以"pattern"開(kāi)頭的行
實(shí)例20:輸出匹配行數(shù)量,而不是匹配行的內(nèi)容
如果只想知道匹配行的數(shù)量,而不是具體的行,可以使用 -c 選項(xiàng):文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-799781.html
[linux@bashcommandnotfound.cn ~]$ grep -c 'pattern' filename
Linux grep命令的注意事項(xiàng)
- 如果搜索字符串中包含特殊字符,你可能要用引號(hào)將搜索字符串括起來(lái);
- grep命令默認(rèn)只對(duì)當(dāng)前目錄下的文件進(jìn)行遞歸搜索,如果你需要在所有子目錄中搜索,需要使用-r或者-R選項(xiàng);
- grep搜索是大小寫(xiě)敏感的,如果需要忽略大小寫(xiě),需要使用-i選項(xiàng)。
- 如果你遇到bash: grep: command not found,那就按照上述方法進(jìn)行安裝。
Linux grep相關(guān)命令
- egrep命令:擴(kuò)展grep,支持更多的正則表達(dá)式
- fgrep命令:速度更快的grep,不支持正則表達(dá)式
- sed命令:流編輯器,用于對(duì)文本文件進(jìn)行復(fù)雜的處理
- awk命令:文本和數(shù)據(jù)處理語(yǔ)言
到了這里,關(guān)于Linux grep命令教程:強(qiáng)大的文本搜索工具(附案例詳解和注意事項(xiàng))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!