grep (global search regular expression(RE) and print out the line,全面搜索正則表達式並把行打印出來)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。php
Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不一樣。egrep是grep的擴展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它們把全部的字母都看做單詞,也就是說,正則表達式中的元字符表示回其自身的字面意義,再也不特殊。linux使用GNU版本的grep。它功能更強,能夠經過-G、-E、-F命令行選項來使用egrep和fgrep的功能。html
全面搜索正則表達式(Global search regular expression(RE) ,GREP)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。mysql
Unix/Linux的grep家族包括grep、egrep和fgrep,其中egrep和fgrep的命令跟grep有細微的區別,egrep是grep的擴展,支持更多的re元字符, fgrep是fixed grep或fast grep簡寫,它們把全部的字母都看做單詞,正則表達式中的元字符表示其自身的字面意義,再也不有其餘特殊的含義,通常使用比較少。linux
目前Linux操做系統默認使用GNU版本的grep。它功能更強,能夠經過-G、-E、-F命令行選項來使用egrep和fgrep的功能。其語法格式及經常使用參數詳解以下:nginx
grep -[acinv] ‘word’ Filenamegit |
參數詳解以下:正則表達式
-c
行數spring
-i
不區分大小寫sql
-n
顯示行號express
-v
取反
-r
遍歷全部子目錄
-A
後面跟數字,過濾出符合要求的行以及下面的n行
-B
同上,過濾出符合要求的行以及上面的n行
-C
同上,過濾出符合要求的行以及上下各n行
-a 以文本文件方式搜索;
-c 計算找到的符合行的次數;
-i 忽略大小寫;
-n 順便輸出行號;
學習Grep時,須要瞭解通配符、正則表達式兩個概念,不少讀者容易把彼此搞混淆,通配符主要用在Linux的Shell命令中,經常使用於文件或者文件名稱的操做,而正則表達式用於文本內容中的字符串搜索和替換,經常使用在AWK、GREP、SED、VIM工具中對文本的操做。
正則表達式元字符
元字符 | 功能 |
---|---|
^ |
以什麼開頭 |
$ |
以什麼結尾 |
. |
匹配一個字符 |
* |
匹配0個或多個 |
[] |
匹配集合中的 |
[x-y] |
匹配集合範圍內的 |
[^ ] |
匹配不在集合中的 |
\ |
轉義 |
特殊的元字符
元字符 | 功能 | 實例 | 怎麼匹配 |
---|---|---|---|
\< |
以什麼開頭 | '\<love' |
匹配以love開頭的全部行 |
\> |
以什麼結尾 | 'love\>' |
匹配love結尾的全部行 |
\(..\) |
標籤匹配之後使用的字符 | '\(love\)able \1er' |
用位置\1\2引導前面作好的標籤,最大支持9個 |
x\{m\} or x\{m,\} or x\{m,n\} |
重複字符x,m次,至少m次,至少m且不超過n次 | o\{5,10\} |
o 字符重複5到10次的行 |
擴展的正則表達式
元字符 | 說明 |
---|---|
+ |
重複前一個字符一個或一個以上 |
? |
0個或者一個字符 |
| |
表示或,查找多個字符串 |
() |
分組過濾匹配 |
通配符類型詳解:
* 0個或者多個字符、數字; ? 匹配任意一個字符; # 表示註解; | 管道符號; ; 多個命令連續執行; |
正則表達式詳解:
* 前一個字符匹配0次或屢次; . 匹配除了換行符之外任意一個字符; .* 表明任意字符; ^ 匹配行首,即以某個字符開頭; $ 匹配行尾,即以某個字符結尾; \(..\) 標記匹配字符; [] 匹配中括號裏的任意指定字符,但只匹配一個字符; [^] 匹配除中括號之外的任意一個字符; |
基本正則表達式元字符 :
字符匹配 :
. : 匹配任意單個字符;
[] : 匹配指定範圍內的任意單個字符;
[^] : 匹配指定範圍外的任意單個字符;
[:digit:]、[:lower:]、[:upper:]、[:alpha:]、[:alnum:]、[:punct:]、[:space:]
匹配次數 : 用在要指定其出現的次數的字符的後面,用於限制其前面字符出現的次數;默認工做於貪婪模式;
* : 匹配其前面的字符任意次;0,1,屢次;
例如 : grep "x*y"
abcd
acbd
* : 匹配任意長度的任意字符
\? : 匹配其前面的字符0次或1次;即其前面的字符是無關緊要的;
\+ : 匹配其前面的字符1次或屢次;即其面的字符要出現至少1次;
\{m\} : 匹配其前面的字符m次;
\{m,n}\ : 匹配其前面的字符至少m次,至多n次;
\{0,n\} : 至多n次;
\{m,\} : 至少n次;
位置錨定 :
^:行首錨定 : 用於模式的最左側;
$:行尾錨定 : 用於模式的最右側;
^PATTERN$ :用於PATTERN來匹配整行;
單詞 : 非特殊字符組成的連續字符(字符串)都稱爲單詞;
\<或\b:詞首錨定,用於單詞模式的左側;
\<或\b:詞尾錨定,用於單詞模式的右側;
\<PATTERN\>:匹配完整單詞;
分組及引用
\(\) : 將一個或多個字符捆綁在一塊兒,看成一個總體進行處理;
\(xy\)*ab
Note : 分組括號中的模式匹配到的內容會被正則表達式引型自動記錄於內部的變量中,這些變量爲:
\1 : 模式從左側起,第一個左括號以及與之匹配的右括號之間的模式所匹配到的字符;
\2 : 模式從左側起,第一個左括號以及與之匹配的右括號之間的模式所匹配到的字符;
\3 :
後向引用 : 引用前面的分組括號中的模式所匹配到的字符;
grep -n 'root' /etc/passwd grep -nv 'nologin' /etc/passwd grep '[0-9]' /etc/passwd grep -v '[0-9]' /etc/passwd grep -v '^#' /etc/passwd grep '^[^a-zA-Z]' /etc/passwd grep 'r.o' /etc/passwd grep '00*' /etc/passwd grep '.*' /etc/passwd grep 'o\{2\}' /etc/passwd grep -E 'o{2}' /etc/passwd grep -E 'o+' /etc/passwd grep -E 'oo?' /etc/passwd grep -E 'root|nologin' /etc/passwd grep -E '(oo){2}' /etc/passwd
grep -c 「test」1.txt 統計test字符總行數;
grep -i 「TEST」 1.txt 不區分大小寫查找TEST全部的行; grep -n 「test」 1.txt 打印test的行及行號; grep -v 「test」1.txt 不打印test的行; grep 「test[53]」 1.txt 以字符test開頭,接5或者3的行; grep 「^[^test]」 jfedu.txt 顯示輸出行首不是test的行; grep 「[Mm]ay」 jfedu.txt 匹配M或m開頭的行; grep 「K…D」 jfedu.txt 匹配K,三個任意字符,緊接D的行; grep -xvf a b | tee c | wc -l 把文件b中有的,可是文件a中沒有的全部行,保存爲文件c,並統計c的行數。
grep -E 'WARNING|FATAL' a.log | grep -v IGNOR | awk -F ":" '{print $5}' 從a.log文件中提取包含"WARNING"或"FATAL",同時不包含"IGNOR"的行,而後提取以":"分割的第5個字段
grep "class" . -R -n 在多級目錄中對文本遞歸搜索 grep -e "class" -e "vitural" file 匹配多個模式 grep "test" file* -lZ| xargs -0 rm grep輸出以做爲結尾符的文件名:(-z)
find source_dir/ -type f -name "*.cpp" -print0 |xargs -0 wc -l 統計程序行數 |
$ grep '^love' re-file love, how much I adore you. Do you know
$ grep 'love$' re-file clover. Did you see them? I can only hope love.
l
開頭,中間包含兩個字符,結尾是e
的全部行$ grep 'l..e' re-file I had a lovely time on our little picnic. love, how much I adore you. Do you know the extent of my love? Oh, by the way, I think I lost my gloves somewhere out in that field of clover. Did you see them? I can only hope love. is forever. I live for you. It's hard to get back in the
love
的字符$ grep ' *love' re-file I had a lovely time on our little picnic. love, how much I adore you. Do you know the extent of my love? Oh, by the way, I think I lost my gloves somewhere out in that field of clover. Did you see them? I can only hope love.
love
或Love
$ grep '[Ll]ove' re-file # 對l不區分大小寫 I had a lovely time on our little picnic. Lovers were all around us. It is springtime. Oh love, how much I adore you. Do you know the extent of my love? Oh, by the way, I think I lost my gloves somewhere out in that field of clover. Did you see them? I can only hope love.
A-Z
的字母,其次是ove
$ grep '[A-Z]ove' re-file Lovers were all around us. It is springtime. Oh
A-Z
範圍內的任何字符行,全部的小寫字符$ grep '[^A-Z]' re-file I had a lovely time on our little picnic. Lovers were all around us. It is springtime. Oh love, how much I adore you. Do you know the extent of my love? Oh, by the way, I think I lost my gloves somewhere out in that field of clover. Did you see them? I can only hope love. is forever. I live for you. It's hard to get back in the groove.
love.
$ grep 'love\.' re-file clover. Did you see them? I can only hope love.
$ grep '^$' re-file
$ grep '.*' re-file I had a lovely time on our little picnic. Lovers were all around us. It is springtime. Oh love, how much I adore you. Do you know the extent of my love? Oh, by the way, I think I lost my gloves somewhere out in that field of clover. Did you see them? I can only hope love. is forever. I live for you. It's hard to get back in the groove.
o
字符重複2到4次$ grep 'o\{2,4\}' re-file groove.
o
字符至少2次$ grep 'o\{2,\}' re-file groove.
0
字符最多2次$ grep 'o\{,2\}' re-file I had a lovely time on our little picnic. Lovers were all around us. It is springtime. Oh love, how much I adore you. Do you know the extent of my love? Oh, by the way, I think I lost my gloves somewhere out in that field of clover. Did you see them? I can only hope love. is forever. I live for you. It's hard to get back in the groove.
$ egrep "go+d" linux.txt Linux is a good god assdxw bcvnbvbjk gooodfs awrerdxxhkl good
ansheng@Ubuntu:/tmp$ egrep "go?d" linux.txt god assdxw bcvnbvbjk gdsystem awxxxx
$ egrep "gd|good" linux.txt Linux is a good gdsystem awxxxx good
$ egrep "g(la|oo)d" linux.txt Linux is a good glad good
使用案例 :
一、顯示/etc/passwd文件中不以/bin/bash結尾的行;
# grep -v "/bin/bash$" /etc/passwd
二、找出/etc/passwd文件中的兩位數或三位數;
#grep "\]<[0-9\{2,3\}\>" /etc/passwd
三、找出/etc/rc.d/rc.sysinit或/etc/grub2.cfg文件中,以致少一個空白字符開頭,且後面非空白字符的行;
# grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg
四、找出「netstat -tan」 命令的結果中以'LISTEN'後跟0、1或多個空白字符結尾的行;
# netstat -tan | grep "LISTEN[[:space:]]*$"
五、找出以root開頭 、root結尾的行;
# grep "^root$" /etc/passwd
六、找出文檔中包含l..e.*l..e的在love.txt中
# grep "l..e.*l..e" love.txt
# grep "\(l..e).*\1" love.txt
七、搜索含有root,hadoop的行
grep -E 「root|hadoop」 /etc/passwd
八、只搜索行首含有root,hadoop的行
grep -E 「^(root|hadoop)\>」 /etc/passwd
九、只搜索行首含有root,hadoop的行,並重定向到/mnt/sysroot/etc/passwd
grep -E 「^(root|hadoop)\>」 /etc/passwd > /mnt/sysroot/etc/passwd
十、查看磁盤大小,並查找以「Disk /dev/[sh]d[a-z]」開頭,只顯示第一行。
答 : disk -l 2> /dev/null | grep "^Disk /dev/[sh]d[a-z]」" | awk -F:'(print $1)'
十一、在用戶目錄下查找文件名後綴爲.yml的文件
find ~ -name '*.yml' | grep '\.yml' --color=always
十二、查找當前目錄下權限爲644的全部文件
find . -perm 644
1三、整個語句是在當前目錄下查找名爲feed.xml的文件,同時須要忽略./_site*路徑的文件。-a -o 實際爲邏輯與邏輯或,當路徑匹配時將執行-prune,那麼將不會查找匹配路徑中的文件,當路徑不匹配時則不執行-prune,-o後的語句始終執行。
find . -path './_site*' -a -prune -o -name 'feed.xml' -print
1四、控制查找的深度
find .-maxdepth 2 -size 3
補充:Linux的權限模式爲三元組「owner」,「group」,「other」,權限對應表以下
1五、tail文本查看命令,能夠看文本的最後幾行。tail命令的優勢在於其內容可以與輸入同步更新,很是適用於查看實時日誌。
-n number
定位參數,+5表示從第五行開始顯示,10或-10表示顯示最後10行
-f
監控文本變化,更新內容
-k number
從number所指的KB處開始讀
範例一:tail -n -5 catalina.out
輸出最後5行
1六、範例二:tail -f catalina.out
監聽catalina.out最後行的變化並顯示
1七、範例三:du -ah --max-depth=1
顯示遞歸的層次爲1,顯示全部文件和文件夾大小
1八、sort -t - -k 1.7 -nk 3,3 sort_k.txt
-k start,end
中end能夠省略,上面的1.7
表示分割後第一個域的第7個字符,因爲沒有end
,則表示對第一個域中第7字符及其以後的字符排序。而3,3
則表示在前面排序的基礎上,再對第三個域進行排序。
sort -nk 2 -t - sort.txt
以-
進行分割,對分割後的第二個域進行排序;
sort -nrk 2 -t - sort.txt
逆序排序
1九、more命令用於顯示文件的內容,與cat和tail等命令不一樣的是,more命令是按頁顯示文件內容,同時具備搜尋字符串的功能。(因爲more具備向前翻頁功能,所以該命令會加載整個文件)
+n
從第n行開始顯示
-n
定義屏幕大小爲n行
+/pattern
再顯示前按pattern匹配子串並顯示
-s
把連續的多個空行顯示爲一行
經常使用操做命令:
Enter 向下n行,默認爲1行
Ctrl+F 跳過一屏
Ctrl+B 返回上一屏
空格鍵 向下滾動一屏
= 輸出當前行的行號
在more模式中回車,輸入/pattern
能夠持續向下搜索
more +/Deploy catalina.out
在catalina.out文件中查找「Deploy字符第一次出現的位置」,並從該處的前兩行開始顯示輸出
more +10 -10 catalina.out
從第10行開始,每頁10行
20、less命令與more命令對應,既能夠先後翻看文件,同時還有先後搜索功能,除此以外,less在查看前不會加載整個文件。
-N
顯示每行的行號
-i
忽略搜索時的大小寫
-s
將連續空行顯示爲一行
-m
顯示百分比
經常使用操做命令:
/字符串 向下搜索「字符串」功能
?字符串 向上搜索「字符串」功能
n 重複前一個搜索
空格鍵 滾動一頁
d 滾動半頁
b 回溯一頁
y 回溯一行
q 退出less命令
範例一:less -Nm catalina.out
顯示行號和百分比
範例二:/detail
或者?detail
向前向後搜索」detail」
2一、find命令實用實例總結
1、基於name查詢文件
# find . -name tecmint.txt
# find /home -name tecmint.txt
# find /home -iname tecmint.txt
# find / -type d -name Tecmint
# find . -type f -name tecmint.php
# find . -type f -name "*.php" #按照文件類型查找,後綴爲.php的文件
2二、2、基於權限查詢文件
# find . -type f -perm 0777 -print
# find / -type f ! -perm 777
# find / -perm 2644
# find / -perm 1551
# find / -perm /u=s
# find / -perm /g+s
# find / -perm /u=r
# find / -perm /a=x
# find / -type f -perm 0777 -print -exec chmod 644 {};
# find / -type d -perm 777 -print -exec chmod 755 {};
# find . -type f -name "tecmint.txt" -execrm -f {} ;
# find . -type f -name "*.txt" -exec rm -f{} ;
# find . -type f -name "*.mp3" -exec rm -f{} ;
# find /tmp -type f -empty
# find /tmp -type d -empty
# find /tmp -type f -name ".*"
2三、3、基於用戶和組查詢文件
# find / -user root -name tecmint.txt
# find /home -user tecmint
# find /home -group developer
# find /home -user tecmint -iname "*.txt"
2四、基於時間查詢文件或目錄
# find / -mtime 50
# find / -atime 50
# find / -mtime +50 –mtime -100
# find / -cmin -60
# find / -mmin -60
# find / -amin -60
2五、基於大小查詢文件或目錄
# find / -size 50M
# find / -size +50M -size -100M
# find / -size +100M -exec rm -rf {} ;
# find / -type f -name *.mp3 -size +10M -exec rm {} ;
2六、使用mindepth和maxdepth限定搜索指定目錄的深度
在root目錄及其1層深的子目錄中查找passwd. (例如root —level 1, and one sub-directory — level 2)
# find -maxdepth 2-name passwd
在root目錄下及其最大兩層深度的子目錄中查找passwd文件. (例如 root — level 1, and two sub-directories — level 2 and 3 )
# find / -maxdepth 3-name passwd
在第二層子目錄和第四層子目錄之間查找passwd文件。
# find -mindepth 3-maxdepth 5 -name passwd
2七、查找5個最大的文件
find . -type f -execls -s {} ; | sort -n -r | head -5
查找5個最小的文件
find . -type f -execls -s {} ; | sort -n | head -5
find . -not -empty-type f -exec ls -s {} ; | sort -n |head -5
2八、查找全部的隱藏文件
find . -type f -name".*"
查找全部的隱藏目錄
find -type d -name".*
2九、下面的命令刪除大於100M的*.zip文件。
find / -type f -name*.zip -size +100M -exec rm -i {} ;"
用別名rm100m刪除全部大雨100M的*.tar文件。使用一樣的思想能夠建立rm1g,rm2g,rm5g的一類別名來刪除全部大於1G,2G,5G的文件。
aliasrm100m="find / -type f -name *.tar -size +100M -exec rm -i {} ;"
# aliasrm1g="find / -type f -name *.tar -size +1G -exec rm -i {} ;"
# aliasrm2g="find / -type f -name *.tar -size +2G -exec rm -i {} ;"
# aliasrm5g="find / -type f -name *.tar -size +5G -exec rm -i {} ;"
30、將/etc/passwd,有出現 root 的行取出來
# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
或
# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
3一、將/etc/passwd,有出現 root 的行取出來,同時顯示這些行在/etc/passwd的行號
# grep -n root /etc/passwd1:root:x:0:0:root:/root:/bin/bash30:operator:x:11:0:operator:/root:/sbin/nologin
3二、將/etc/passwd,將沒有出現 root 和nologin的行取出來
# grep -v root /etc/passwd | grep -v nologin root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
3三、用 dmesg 列出核心信息,再以 grep 找出內含 eth 那行,要將捉到的關鍵字顯色,且加上行號來表示:
[root@localhost ~]# dmesg | grep -n --color=auto 'eth'247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0: Identified 8139 chip type 'RTL-8139C'294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1305:eth0: no IPv6 routers present # 你會發現除了 eth 會有特殊顏色來表示以外,最前面還有行號喔!
3四、根據文件內容遞歸查找目錄
# grep ‘energywise’ * #在當前目錄搜索帶'energywise'行的文件 # grep -r ‘energywise’ * #在當前目錄及其子目錄下搜索'energywise'行的文件# grep -l -r ‘energywise’ * #在當前目錄及其子目錄下搜索'energywise'行的文件,可是不顯示匹配的行,只顯示匹配的文件
3五、找出空白行:
[root@localhost ~]# grep -n '^$' regular_express.txt22:
3六、不想要開頭是英文字母
[root@localhost ~]# grep -n '^[^a-zA-Z]' regular_express.txt1
3七、想要找出來,行尾結束爲小數點 (.) 的那一行:
[root@localhost ~]# grep -n '\.$' regular_express.txt1
3八、『至少兩個 o 以上的字串』時,就須要 ooo* ,亦便是:
[root@localhost ~]# grep -n 'ooo*' regular_express.txt1
3九、想要字串開頭與結尾都是 g,可是兩個 g 之間僅能存在至少一個 o ,亦便是 gog, goog, gooog.... 等等,那該如何?
[root@localhost ~]# grep -n 'goo*g' regular_express.txt18
40、若是我想要找出 g 開頭與 g 結尾的行,當中的字符無關緊要
[root@localhost ~]# grep -n 'g.*g' regular_express.txt1
4一、要找出 g 後面接 2 到 5 個 o ,而後再接一個 g 的字串,他會是這樣:
[root@localhost ~]# grep -n 'go\{2,5\}g' regular_express.txt18:google is the best tools for search keyword.
4二、想要的是 2 個 o 以上的 goooo....g 呢?除了能夠是 gooo*g ,也能夠是:
[root@localhost ~]# grep -n 'go\{2,\}g' regular_express.txt18
4三、打印全部包含NW或EA的行。若是不是使用egrep,而是grep,將不會有結果查出。
# egrep 'NW|EA' testfile
4四、搜索全部包含一個或多個3的行。
# egrep '3+' testfile # grep -E '3+' testfile # grep '3\+' testfile
4五、搜索全部包含0個或1個小數點字符的行。
# egrep '2\.?[0-9]' testfile # grep -E '2\.?[0-9]' testfile # grep '2\.\?[0-9]' testfile
4六、搜索一個或者多個連續的no的行。
# egrep '(no)+' testfile # grep -E '(no)+' testfile # grep '\(no\)\+' testfile #3個命令返回相同結果,
4七、若是你想在一個文件或者輸出中找到包含星號字符的行
fgrep '*' /etc/profile for i in /etc/profile.d/*.sh ; do 或 grep -F '*' /etc/profile for i in /etc/profile.d/*.sh ; do
4八、將/etc/passwd,有出現root的行取出來
# grep root /etc/passwd
或
# cat /etc/passwd | grep root
4九、將/etc/passwd,有出現 root 的行取出來,同時顯示這些行在/etc/passwd的行號
# grep -n root /etc/passwd1:root:x:0:0:root:/root:/bin/bash30:operator:x:11:0:operator:/root:/sbin/nologin
50、將/etc/passwd,將沒有出現 root 的行取出來
# grep -v root /etc/passwdroot:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
將/etc/passwd,將沒有出現 root 和nologin的行取出來
# grep -v root /etc/passwd | grep -v nologin root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
用 dmesg 列出核心信息,再以 grep 找出內含 eth 那行,要將捉到的關鍵字顯色,且加上行號來表示:
[root@localhost ~]# dmesg | grep -n --color=auto 'eth'247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0: Identified 8139 chip type 'RTL-8139C'294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1305:eth0: no IPv6 routers present # 你會發現除了 eth 會有特殊顏色來表示以外,最前面還有行號喔!
用 dmesg 列出核心信息,再以 grep 找出內含 eth 那行,在關鍵字所在行的前兩行與後三行也一塊兒捉出來顯示
[root@localhost ~]# dmesg | grep -n -A3 -B2 --color=auto 'eth'245-PCI: setting IRQ 10 as level-triggered246-ACPI: PCI Interrupt 0000:00:0e.0[A] -> Link [LNKB] ...247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0: Identified 8139 chip type 'RTL-8139C'249-input: PC Speaker as /class/input/input2250-ACPI: PCI Interrupt 0000:00:01.4[B] -> Link [LNKB] ...251-hdb: ATAPI 48X DVD-ROM DVD-R-RAM CD-R/RW drive, 2048kB Cache, UDMA(66) # 如上所示,你會發現關鍵字 247 所在的前兩行及 248 後三行也都被顯示出來! # 這樣可讓你將關鍵字先後數據捉出來進行分析啦!
5一、根據文件內容遞歸查找目錄
# grep ‘energywise’ * #在當前目錄搜索帶'energywise'行的文件 # grep -r ‘energywise’ * #在當前目錄及其子目錄下搜索'energywise'行的文件# grep -l -r ‘energywise’ * #在當前目錄及其子目錄下搜索'energywise'行的文件,可是不顯示匹配的行,只顯示匹配的文件
5二、字符類
符類的搜索:若是我想要搜尋 test 或 taste 這兩個單字時,能夠發現到,其實她們有共通的 't?st' 存在~這個時候,我能夠這樣來搜尋:
[root@localhost~]# grep -n 't[ae]st' regular_express.txt8:I can't finish the test.9:Oh! The soup taste good.
字符類的反向選擇 [^] :若是想要搜索到有 oo 的行,但不想要 oo 前面有 g,以下 :
[root@localhost ~]# grep -n '[^g]oo' regular_express.txt2:apple is my favorite food.3:Football game is not use feet only.18:google is the best tools for search keyword.19:goooooogle yes!
5三、行首與行尾字節 ^ $
行首字符:若是我想要讓 the 只在行首列出呢? 這個時候就得要使用定位字節了!咱們能夠這樣作:
[root@ocalhost ~]# grep -n '^the' regular_express.txt12:the symbol '*' is represented as start.
想要開頭是小寫字節的那一行就列出呢?能夠這樣:
[root@ocalhost ~]# grep -n '^[a-z]' regular_express.txt
2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
不想要開頭是英文字母,則能夠是這樣:
[root@localhost ~]# grep -n '^[^a-zA-Z]' regular_express.txt1:"Open Source" is a good mechanism to develop programs.21:# I am VBird
想要找出來,行尾結束爲小數點 (.) 的那一行:
[root@www ~]# grep -n '\.$' regular_express.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.4:this dress doesn't fit
找出空白行:
[root@www ~]# grep -n '^$' regular_express.txt22:
由於只有行首跟行尾 (^$),因此,這樣就能夠找出空白行啦!
須要找出 g??d 的字串,亦即共有四個字節, 起頭是 g 而結束是 d ,我能夠這樣作:
[root@localhost ~]# grep -n 'g..d' regular_express.txt1:"Open Source" is a good mechanism to develop programs.9:Oh! The soup taste good.16:The world <Happy> is the same with "glad".
須要『至少兩個 o 以上的字串』時,就須要 ooo* ,亦便是:
[root@www ~]# grep -n 'ooo*' regular_express.txt1:"Open Source" is a good mechanism to develop
想要字串開頭與結尾都是 g,可是兩個 g 之間僅能存在至少一個 o ,亦便是 gog, goog, gooog.... 等等,那該如何?
[root@localhost ~]# grep -n 'goo*g' regular_express.txt18:google is the best tools for search keyword.19:goooooogle yes!
想要找出 g 開頭與 g 結尾的行,當中的字符無關緊要
[root@localhost ~]# grep -n 'g.*g' regular_express.txt1:"Open Source" is a good mechanism to develop programs.14:The gd software is a library for drafting programs.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.
想要找出『任意數字』的行?由於僅有數字,因此就成爲:
[root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt5:However, this dress is about $ 3183 dollars.15:You are the best is mean you are the no. 1.
假設要找到兩個 o 的字串,能夠是:
[root@www ~]# grep -n 'o\{2\}' regular_express.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! The soup taste good.18:google is the best tools for search ke19:goooooogle yes!
假設要找出 g 後面接 2 到 5 個 o ,而後再接一個 g 的字串,他會是這樣:
[root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt18:google is the best tools for search keyword.
若是想要的是 2 個 o 以上的 goooo....g 呢?除了能夠是 gooo*g ,也能夠是:
[root@www ~]# grep -n 'go\{2,\}g' regular_express.txt18:google is the best tools for search keyword.19:goooooogle yes!
5四、擴展grep(grep -E 或者 egrep):
打印全部包含NW或EA的行。若是不是使用egrep,而是grep,將不會有結果查出。
# egrep 'NW|EA' testfile northwest NW Charles Main 3.0 .98 3 34 eastern EA TB Savage 4.4 .84 5 20
對於標準grep,若是在擴展元字符前面加\,grep會自動啓用擴展選項-E。
#grep 'NW\|EA' testfile northwest NW Charles Main 3.0 .98 3 34eastern EA TB Savage 4.4 .84 5 20
搜索全部包含一個或多個3的行。
# egrep '3+' testfile # grep -E '3+' testfile # grep '3\+' testfile #這3條命令將會 northwest NW Charles Main 3.0 .98 3 34western WE Sharon Gray
搜索全部包含0個或1個小數點字符的行。
# egrep '2\.?[0-9]' testfile # grep -E '2\.?[0-9]' testfile # grep '2\.\?[0-9]' testfile #首先含有2字符,其後緊跟着0個或1個點,後面再是0和9之間的數字。 western WE Sharon Gray 5.3 .97
搜索一個或者多個連續的no的行。
# egrep '(no)+' testfile # grep -E '(no)+' testfile # grep '\(no\)\+' testfile #3個命令返回相同結果, northwest NW Charles Main 3.0 .98
若是你想在一個文件或者輸出中找到包含星號字符的行
fgrep '*' /etc/profile for i in /etc/profile.d/*.sh ; do 或 grep -F '*' /etc/profile for i in /etc/profile.d/*.sh ; do
5五、查找特定進程
[root@localhost ~]# ps -aux | grep mysql
###查找含有mysql關鍵字的進程
5六、搜索含有關鍵字的行並高亮
[root@localhost /search/nginx/html]# cat index.php | grep -n test --color=always
5七、從多個文件的內容中查找含有關鍵字的文件
[root@localhost /search/nginx/html]# grep -r p3p.sogou.com /usr/local/
5八、動態查看文件更新含有特定關鍵字的內容並高亮
[root@localhost /search/nginx/html]# tail -f /search/nginx/logs/access_log | grep favicon --color=always
5九、日誌篩選處理
如下命令實現了在當前目錄下對access1.log文件進行查找,找到那些不包含404的行,把它們放到access2.log中,後面去掉’v’,便是把有404的行放入access2.log
[root@localhost /search/nginx/logs]# cat access_log | grep -v "404" >access_no404.log
60、
6一、
6二、
6三、
6四、
6五、
6六、
6七、
6八、
6九、
70、
7一、
7二、
參考連接 : https://mp.weixin.qq.com/s/6VYDEf8Wbd28T2QMVEMp1g
Linux正則表達式範例 : https://blog.ansheng.me/article/examples-of-linux-regular-expressions/