【Linux】一步一步学Linux——grep命令(49)
00. 目錄
文章目錄
- 00. 目錄
- 01. 命令概述
- 02. 命令格式
- 03. 常用選項
- 04. 參考示例
- 05. 附錄
01. 命令概述
Linux系統中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹 配的行打印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權限是所有用戶。
grep命令的選項用于對搜索過程的補充,而其命令的模式十分靈活,可以是變量、字符串、正則表達式。需要注意的是:一當模式中包含了空格,務必要用雙引號將其引起來。
linux系統支持三種形式的grep命令,大兒子就是grep,標準,模仿的代表。二兒子興趣愛好多-egrep,簡稱擴展grep命令,其實和grep -E等價,支持基本和擴展的正則表達式。小兒子跑的最快-fgrep,簡稱快速grep命令,其實和grep -F等價,不支持正則表達式,按照字符串表面意思進行匹配。
02. 命令格式
grep [options] PATTERN [FILE...] grep [options] [-e PATTERN | -f FILE] [FILE...]03. 常用選項
匹配模式選擇:-E, --extended-regexp 擴展正則表達式egrep-F, --fixed-strings 一個換行符分隔的字符串的集合fgrep-G, --basic-regexp 基本正則-P, --perl-regexp 調用的perl正則-e, --regexp=PATTERN 后面根正則模式,默認無-f, --file=FILE 從文件中獲得匹配模式-i, --ignore-case 不區分大小寫-w, --word-regexp 匹配整個單詞-x, --line-regexp 匹配整行-z, --null-data 一個 0 字節的數據行,但不是空行雜項:-s, --no-messages 不顯示錯誤信息-v, --invert-match 顯示不匹配的行-V, --version 顯示版本號--help 顯示幫助信息--mmap use memory-mapped input if possible輸入控制:-m, --max-count=NUM 匹配的最大數-b, --byte-offset 打印匹配行前面打印該行所在的塊號碼。-n, --line-number 顯示的加上匹配所在的行號--line-buffered 刷新輸出每一行-H, --with-filename 當搜索多個文件時,顯示匹配文件名前綴-h, --no-filename 當搜索多個文件時,不顯示匹配文件名前綴--label=LABEL print LABEL as filename for standard input-o, --only-matching 只顯示一行中匹配PATTERN 的部分-q, --quiet, --silent 不顯示任何東西--binary-files=TYPE 假定二進制文件的TYPE 類型;TYPE 可以是`binary', `text', 或`without-match'-a, --text 匹配二進制的東西-I 不匹配二進制的東西-d, --directories=ACTION 目錄操作,讀取,遞歸,跳過-D, --devices=ACTION 設置對設備,FIFO,管道的操作,讀取,跳過-R, -r, --recursive 遞歸調用--include=PATTERN 只查找匹配FILE_PATTERN 的文件--exclude=PATTERN 跳過匹配FILE_PATTERN 的文件和目錄--exclude-from=FILE 跳過所有除FILE 以外的文件-L, --files-without-match 匹配多個文件時,顯示不匹配的文件名-l, --files-with-matches 匹配多個文件時,顯示匹配的文件名-c, --count 顯示匹配的行數-Z, --null 在FILE 文件最后打印空字符文件控制:-B, --before-context=NUM 打印匹配本身以及前面的幾個行由NUM控制-A, --after-context=NUM 打印匹配本身以及隨后的幾個行由NUM控制-C, --context=NUM 打印匹配本身以及隨后,前面的幾個行由NUM控制-NUM 根-C的用法一樣的--color[=WHEN],--colour[=WHEN] 使用標志高亮匹配字串;-U, --binary 使用標志高亮匹配字串;-u, --unix-byte-offsets 當CR 字符不存在,報告字節偏移(MSDOS 模式)規則表達式: grep的規則表達式:^ #錨定行的開始 如:'^grep'匹配所有以grep開頭的行。 $ #錨定行的結束 如:'grep$'匹配所有以grep結尾的行。 . #匹配一個非換行符的字符 如:'gr.p'匹配gr后接一個任意字符,然后是p。 * #匹配零個或多個先前字符 如:'*grep'匹配所有一個或多個空格后緊跟grep的行。 .* #一起用代表任意字符。 [] #匹配一個指定范圍內的字符,如'[Gg]rep'匹配Grep和grep。 [^] #匹配一個不在指定范圍內的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一個字母開頭,緊跟rep的行。 \(..\) #標記匹配字符,如'\(love\)',love被標記為1。 \< #錨定單詞的開始,如:'\<grep'匹配包含以grep開頭的單詞的行。 \> #錨定單詞的結束,如'grep\>'匹配包含以grep結尾的單詞的行。 x\{m\} #重復字符x,m次,如:'0\{5\}'匹配包含5個o的行。 x\{m,\} #重復字符x,至少m次,如:'o\{5,\}'匹配至少有5個o的行。 x\{m,n\} #重復字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10個o的行。 \w #匹配文字和數字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零個或多個文字或數字字符,然后是p。 \W #\w的反置形式,匹配一個或多個非單詞字符,如點號句號等。 \b #單詞鎖定符,如: '\bgrep\b'只匹配grep。 POSIX字符: 為了在不同國家的字符編碼中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符類,如[:alnum:]是[A-Za-z0-9]的另一個寫法。要把它們放到[]號內才能成為正則表達式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符類。[:alnum:] #文字數字字符 [:alpha:] #文字字符 [:digit:] #數字字符 [:graph:] #非空字符(非空格、控制字符) [:lower:] #小寫字符 [:cntrl:] #控制字符 [:print:] #非空字符(包括空格) [:punct:] #標點符號 [:space:] #所有空白字符(新行,空格,制表符) [:upper:] #大寫字符 [:xdigit:] #十六進制數字(0-9,a-f,A-F)04. 參考示例
4.1 文件中搜索一個單詞
[deng@localhost share]$ grep "root" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [deng@localhost share]$4.2 多文件中查找單詞
[deng@localhost test]$ grep "root" passwd passwd1 passwd:root:x:0:0:root:/root:/bin/bash passwd:operator:x:11:0:operator:/root:/sbin/nologin passwd1:root:x:0:0:root:/root:/bin/bash passwd1:operator:x:11:0:operator:/root:/sbin/nologin [deng@localhost test]$4.3 輸出除匹配之外的所有行
[deng@localhost test]$ grep -v "root" /etc/passwd4.4 匹配部分顏色顯示
[deng@localhost test]$ grep "root" /etc/passwd --color=auto root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [deng@localhost test]$4.5 使用正則表達式
[deng@localhost test]$ grep -E "[1-9]+" /etc/passwd或者
[deng@localhost test]$ egrep "[1-9]+" /etc/passwd4.6 只輸出文件中匹配到的部分
[deng@localhost test]$ grep -o "root" /etc/passwd root root root root [deng@localhost test]$4.7 統計文件或者文本中包含匹配字符串的行數
[deng@localhost test]$ grep -c "root" /etc/passwd 24.8 輸出包含匹配字符串的行號
[deng@localhost test]$ grep -n "root" /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin [deng@localhost test]$多個文件匹配情況
[deng@localhost test]$ grep -n "root" passwd passwd1 passwd:1:root:x:0:0:root:/root:/bin/bash passwd:10:operator:x:11:0:operator:/root:/sbin/nologin passwd1:1:root:x:0:0:root:/root:/bin/bash passwd1:10:operator:x:11:0:operator:/root:/sbin/nologin [deng@localhost test]$4.9 輸出字符或字節偏移
-b顯示字符或者字節偏移
[deng@localhost test]$ grep -b -o "root" /etc/passwd 0:root 11:root 17:root 366:root [deng@localhost test]$4.10 搜索多個文件并輸出查找匹配文本文件
[deng@localhost test]$ grep -l "root" passwd passwd1 passwd passwd1 [deng@localhost test]$4.11 在多級目錄中對文本進行遞歸搜索
[deng@localhost test]$ grep -r "root" /etc4.12 忽略大小寫搜索
[deng@localhost test]$ grep -i "ROOT" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [deng@localhost test]$4.13 同時指定多個匹配的字符串
[deng@localhost test]$ grep -e "root" -e "deng" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin deng:x:1000:1000:deng:/home/deng:/bin/bash [deng@localhost test]$4.14 在grep搜索結果中包括或者排除指定文件
在目錄中所有的.php和.html文件中遞歸搜索字符"main"
[deng@localhost test]$ grep -r "main" ./ --include *.{php,html}搜索結果中排除所有README文件
[deng@localhost test]$ grep -r "main" ./ --exclude "README"搜索結果中排除filelist文件列表里的文件
[deng@localhost test]$ grep -r "main" ./ --exclude-from filelist4.15 不顯示任何東西
[deng@localhost test]$ grep -q "root" /etc/passwd4.16 使用0值字節后綴的grep與xargs
#測試文件: echo "aaa" > file1 echo "bbb" > file2 echo "aaa" > file3grep "aaa" file* -lZ | xargs -0 rm#執行后會刪除file1和file3,grep輸出用-Z選項來指定以0值字節作為終結符文件名(\0),xargs -0 讀取輸入并用0值字節終結符分隔文件名,然后刪除匹配文件,-Z通常和-l結合使用。4.17 打印出匹配文本之前或者之后的行
顯示匹配某個結果之后的3行,使用 -A 選項
[deng@localhost test]$ seq 1 10 | grep "5" -A 3 5 6 7 8 [deng@localhost test]$顯示匹配某個結果之前的3行,使用 -B 選項
[deng@localhost test]$ seq 1 10 | grep "5" -B 3 2 3 4 5 [deng@localhost test]$顯示匹配某個結果的前三行和后三行,使用 -C 選項
[deng@localhost test]$ seq 1 10 | grep "5" -C 3 2 3 4 5 6 7 8 [deng@localhost test]$匹配結果有多個,會用“–”作為各匹配結果之間的分隔符
[deng@localhost test]$ echo -e "a\nb\nc\na\nb\nc" | grep a -A 1 a b -- a b4.18 查找一個文件中的空行和非空行
統計文件中空行的行數
[deng@localhost test]$ grep -c ^$ /etc/passwd統計文件中非空行的行數
[deng@localhost test]$ grep -c ^[^$] /etc/passwd 46 [deng@localhost test]$4.19 輸出所有以root開頭的行
[deng@localhost test]$ grep ^root /etc/passwd root:x:0:0:root:/root:/bin/bash [deng@localhost test]$4.20 輸出所有以bash結尾的行
[deng@localhost test]$ grep bash$ /etc/passwd root:x:0:0:root:/root:/bin/bash deng:x:1000:1000:deng:/home/deng:/bin/bash oracle:x:1001:1002::/home/oracle:/bin/bash itcast:x:1002:1003::/home/itcast:/bin/bash [deng@localhost test]$4.21 從文件中讀取關鍵字搜索
[deng@localhost test]$ grep -f file /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [deng@localhost test]$ cat file root [deng@localhost test]$說明:輸出/etc/passwd文件中含有從file文件中讀取出的關鍵詞的內容行
4.22 支持正則表達式
顯示當前目錄下面以.txt 結尾的文件中的所有包含每個字符串至少有7個連續小寫字符的字符串的行
[deng@localhost test]$ grep '[a-z]\{7\}' *.txt4.23 顯示所有匹配root和deng的行
[deng@localhost test]$ grep -E "root|deng" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin deng:x:1000:1000:deng:/home/deng:/bin/bash [deng@localhost test]$4.24 匹配IP地址
X\{m,n\} 匹配字符X m—n 次
[deng@localhost test]$ ifconfig ens33 |grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"inet 192.168.1.104 netmask 255.255.255.0 broadcast 192.168.1.255 [deng@localhost test]$4.25 匹配root或者deng或者itcast
[deng@localhost test]$ grep '\(root\|deng\|itcast\)' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin deng:x:1000:1000:deng:/home/deng:/bin/bash itcast:x:1002:1003::/home/itcast:/bin/bash [deng@localhost test]$4.26 X\{m,\} 匹配字符X 最少m次
[deng@localhost test]$ grep 'o\{2,\}' /etc/passwd root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin [deng@localhost test]$4.27 X\{m\}匹配字符X m次
[deng@localhost test]$ grep 'o\{2\}' /etc/passwd root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin [deng@localhost test]$4.28 \+ 匹配前面的字符或者字符串1次或者多次
[deng@localhost test]$ grep 'root\+' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin4.29 ? 匹配前面的字符或者字符串0 次或者 多次
[deng@localhost test]$ grep 'root\?' /etc/passwd [deng@localhost test]$ grep 'root\?' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [deng@localhost test]$4.30 \用來屏蔽元字符的特殊含義
//匹配. [deng@localhost test]$ ifconfig ens33 | grep '\.'inet 192.168.1.104 netmask 255.255.255.0 broadcast 192.168.1.255RX packets 102516085 bytes 13627338339 (12.6 GiB)TX packets 145027 bytes 41135471 (39.2 MiB) [deng@localhost test]$4.31 匹配 [ ]內的字符
如 [ 1] 即匹配含有字符’1’的字符串
[deng@localhost test]$ grep [1] /etc/passwd如 [ a] 即匹配含有字符’a’的字符串
[deng@localhost test]$ grep '[a]' /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin如 [123] 即匹配含有字符’1’ 或者 ’2’ 或者’3’ 的字符串
[deng@localhost test]$ grep '[123]' /etc/passwd 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也可以使用字符序列,用字符 ‘-’ 代表字符序
如 [ 1-3 ] 即匹配含有字符’1’ 或者 ’2’ 或者’3’ 的字符串
[deng@localhost test]$ grep '[1-3]' /etc/passwd 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 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin如 [ 1-3 a-b] 即匹配含有字符’1’ 或者 ’2’ 或者’3’ 或者 ’a’ 或者 ’b’的字符串
[deng@localhost test]$ grep '[1-3a-b]' /etc/passwd4.32 .匹配任意的單字符
任意兩個字符開頭,然后第三個字符為 ‘3’
[deng@localhost test]$ grep '^..3' /etc/passwd05. 附錄
參考:【Linux】一步一步學Linux系列教程匯總
總結
以上是生活随笔為你收集整理的【Linux】一步一步学Linux——grep命令(49)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux】一步一步学Linux——f
- 下一篇: 【Linux】一步一步学Linux——e