[轉] linux 查找文本過濾grep正則表達式命令詳解用法

  grep (global search regular expression(RE) and print out the line,全面搜索正則表達式並把行打印出來)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。css

  Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不一樣。egrep是grep的擴展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它們把全部的字母都看做單詞,也就是說,正則表達式中的元字符表示回其自身的字面意義,再也不特殊。linux使用GNU版本的grep。它功能更強,能夠經過-G、-E、-F命令行選項來使用egrepfgrephtml


      過濾文本grepmysql

  grep是一種強大的文本搜索工具命令,用於查找文件中的符合指定格式的字符串,支持正則表達式。如不指定任何文件名稱,或是所給予的文件名爲「—」,則grep命令從標準輸入設備讀取數據。grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不一樣。egrep是grep的擴展。fgrep就是fixed grep或fast grep,該命令使用任何正則表達式中的元字符表示其自身的字面意義,再也不特殊。其中egrep就等同於「grep —E」,fgrep等同於「grep -F」。Linux中的grep功能強大,支持不少豐富的參數,能夠方便地進行一些文本處理工做。linux

  grep單獨使用時至少有兩個參數,如少於兩個參數,grep會一直等待,直到該程序被中斷。若是遇到了這樣的狀況,能夠按「Ctrl+c」終止。默認狀況下只搜索當前目錄,若是遞歸查找子目錄,可以使用「r」選項。nginx

#在指定文件中查找特定字符串

[root@CentOS ~]# grep root /etc/passwd

#結合管道一塊兒使用

[root@CentOS ~]# cat /etc/passwd | grep root

#將顯示符合條件的內容所在的行號

[root@CentOS ~]# grep -n root /etc/passwd

#在nginx.conf查找包含listen的行號打印出來

[root@CentOS conf]# grep listen nginx.conf

#結合管道聯合使用,其中/sbin/ifconfig 表示查看當前系統的網絡配置信息,後查找包含「inetaddr"的字符串,第2行爲查找的結果

[root@CentOS etc]# cat filel

[root@CentOS etc]# grep var filel

[root@CentOS etc]# grep -v var filel

#顯示行號

[root@CentOS etc]# grep -n var filel

[root@CentOS nginx]#  /sbin/ifconfig | grep "inet addr"

#綜合使用

 $ grep magic /usr/src/linux/Documentation/* | tail

#查看文件內容

[root@CentOS etc]# cat test.txt

#查找指定字符串,此時是區分大小寫

[root@CentOS etc]# grep uuid test.txt

[root@CentOS etc]# grep UUID test.txt

#不區分大小寫查找指定字符串

[root@CentOS etc]# grep -i uuid test.txt

#列出匹配字符串的文件名

[root@CentOS etc]# grep -l UUID test.txt

[root@CentOS etc]# grep -L UUID test.txt

#列出不匹配字符串的文件名

[root@CentOS etc]# grep -L uuid test.txt

#匹配整個單詞

[root@CentOS etc]# grep -W UU test.txt

[root@CentOS etc]# grep -W UUID test.txt

#除了顯示匹配的行,分別顯示該行上下文的N行

[root@CentOS etc]# grep -C1 UUID test.txt

[root@CentOS etc]# grep -n -E "^[a-z]+" test.txt

[root@CentOS etc]# grep -n -E "^[^a-z]+" test.txt

#按正則表達式查找指定字符串

[root@CentOS etc]# cat my.cnf

#按正則表達式查找

[root@CentOS etc]#  grep -E "datadir|socket"my.cnf

[root@CentOS etc]#  grep mysql my.cnf

#結合管道一塊兒使用

[root@CentOS etc]#  grep mysql my.cnf | grep datadir

#遞歸查找

[root@CentOS etc]#  grep -r var .|head -3

反向查找,文件名中包含test 的文件中不包含test 的行git

grep -v test*正則表達式

例:在文件kkk中搜索匹配字符「test file」。sql

[root@rhel ~]# grep 'test file' kkk test fileexpress

例:在文件aa中顯示全部包含至少有5個連續小寫字符的行數據內容。網絡

[root@rhel ~]# grep '[a-z]\{5\}' aa aaaaa aaaaaa

例:在/root/aa文件中找出以b開頭的行內容。

[root@rhel ~]# grep ^b /root/aa bbb

例:在/root/aa文件中輸出不是以b開頭的行內容。

[root@rhel ~]# grep -v ^b /root/aa aaaaa AAAAA BBB aaaaaa

例:在/root/kkk文件中輸出以le結尾的行內容。

[root@rhel ~]# grep le$ /root/kkk test file

在stdout1.log文件中查找有'exception'的行。

grep 'exception' stdout1.log

在stdout1.log文件中查找有'exception'的行的數目。

grep -c 'exception' stdout1.log

grep不顯示自己進程

ps aux|grep \[s]sh

ps aux | grep ssh | grep -v "grep"

輸出ip地址

ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"

顯示當前目錄下面以.txt 結尾的文件中的全部包含每一個字符串至少有7個連續小寫字符的字符串的行

grep '[a-z]\{7\}' *.txt

日誌文件過大,很差查看,咱們要從中查看本身想要的內容,或者獲得同一類數據,好比說沒有404日誌信息的

grep '.' access1.log|grep -Ev '404' > access2.log

grep '.' access1.log|grep -Ev '(404|/photo/|/css/)' > access2.log

grep '.' access1.log|grep -E '404' > access2.log

grep支持豐富的正則表達式,常見的正則元字符含義表

grep正則參數說明

 參數  說明
 ^  指定匹配字符串的行首
 $  指定匹配字符串的結尾
 *  表示0個以上的字符
 +  表示1個以上的字符
 \  去掉指定字符的特殊含義
 ^  指定行的開始
 $  指定行的結束
 .  匹配一個非換行符的字符
 *  匹配零個或多個先前字符
 []  匹配一個指定範圍內的字符
 [^]  匹配一個不在指定範圍內的字符
 \(..\)  標記匹配字符
 <  指定單詞的開始
 >  指定單詞的結束
 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]
 b  單詞鎖定符
 +  匹配一個或多個先前的字符
 ?  匹配零個或多個先前的字符
 a|b|c  匹配a或b或c
 ()  分組符號
 [:alnum:]  文字數字字符
 [:alpha:]  文字字符
 [:digit:]  數字字符
 [:graph:]  非空格、控制字符
 [:lower:]  小寫字符
 [:cntrl:]  控制字符
 [:print:]  非空字符(包括空格)
 [:punct:]  標點符號
 [:space:]  全部空白字符(新行,空格,製表符)
 [:upper:]  大寫字符
 [:xdigit:]  十六進制數字(0-9,a-f,A-F)
   
   

 

  匹配的實例:

  grep -c "48" test.txt 統計全部以「48」字符開頭的行有多少

  grep -i "May" test.txt 不區分大小寫查找「May」全部的行)

  grep -n "48" test.txt 顯示行號;顯示匹配字符「48」的行及行號,相同於 nl test.txt |grep 48)

  grep -v "48" test.txt 顯示輸出沒有字符「48」全部的行)

  grep "471" test.txt 顯示輸出字符「471」所在的行)

  grep "48;" test.txt 顯示輸出以字符「48」開頭,並在字符「48」後是一個tab鍵所在的行

  grep "48[34]" test.txt 顯示輸出以字符「48」開頭,第三個字符是「3」或是「4」的全部的行)

  grep "^[^48]" test.txt 顯示輸出行首不是字符「48」的行)

  grep "[Mm]ay" test.txt 設置大小寫查找:顯示輸出第一個字符以「M」或「m」開頭,以字符「ay」結束的行)

  grep "K…D" test.txt 顯示輸出第一個字符是「K」,第2、3、四是任意字符,第五個字符是「D」所在的行)

  grep "[A-Z][9]D" test.txt 顯示輸出第一個字符的範圍是「A-D」,第二個字符是「9」,第三個字符的是「D」的全部的行

  grep "[35]..1998" test.txt 顯示第一個字符是3或5,第二三個字符是任意,以1998結尾的全部行

  grep "4/{2,/}" test.txt 模式出現概率查找:顯示輸出字符「4」至少重複出現兩次的全部行

  grep "9/{3,/}" test.txt 模式出現概率查找:顯示輸出字符「9」至少重複出現三次的全部行

  grep "9/{2,3/}" test.txt 模式出現概率查找:顯示輸出字符「9」重複出現的次數在必定範圍內,重複出現2次或3次全部行

  grep -n "^$" test.txt 顯示輸出空行的行號

  ls -l |grep "^d" 若是要查詢目錄列表中的目錄 同:ls -d *

  ls -l |grep "^d[d]" 在一個目錄中查詢不包含目錄的全部文件

  ls -l |grpe "^d…..x..x" 查詢其餘用戶和用戶組成員有可執行權限的目錄集合

  更多的例子:

  搜索有the的行,並輸出行號

  $grep -n 'the' regular_express.txt

  搜 索沒有the的行,並輸出行號

  $grep -nv 'the' regular_express.txt

  利 用[]搜索集合字符

  [] 表示其中的某一個字符 ,例如[ade] 表示a或d或e

  woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_express.txt

  8:I can't finish the test.

  9:Oh! the soup taste good!

  能夠用^符號作[]內的前綴,表示除[]內的字符以外的字 符。

  好比搜索oo前沒有g的字符串所在的行. 使用 '[^g]oo' 做搜索字符串

  woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt

  2: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!

  內能夠用範圍表示,好比[a-z] 表示小寫字母,[0-9] 表示0~9的數字, [A-Z] 則是大寫字母們。[a-zA-Z0-9]表示全部數字與英文字符。 固然也能夠配合^來排除字符。

  搜索包含數字的行

  woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt

  5:However ,this dress is about $ 3183 dollars.

  15:You are the best is menu you are the no.1.

  行首與行尾字符 ^ $. ^ 表示行的開頭,$表示行的結尾( 不是字符,是位置)那麼‘^$’ 就表示空行,由於只有行首和行尾。

  這裏^與[]裏面使用的^意義不一樣。它表示^後面的串是在行的開頭。

  好比搜索the在開頭的行

  woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt

  12:the symbol '*' is represented as star.

  搜索以小寫字母開頭的行

  woody@xiaoc:~/tmp$ 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 star.

  18:google is the best tools for search keyword.

  19:goooooogle yes!

  20:go! go! Let's go.

  woody@xiaoc:~/tmp$

  搜索開頭不是英文字母的行

  woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt

  1:"Open Source" is a good mechanism to develop programs.

  21:#I am VBird

  woody@xiaoc:~/tmp$

  $表示它前面的串是在行的結尾,好比 '/.' 表示 . 在一行的結尾

  搜索末尾是.的行

  woody@xiaoc:~/tmp$ grep -n '/.$' regular_express.txt //. 是正則表達式的特殊符號,因此要用/轉義

  1:"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 me.

  5:However ,this dress is about $ 3183 dollars.

  6:GNU is free air not free beer.

  注意在MS的系統下生成的文本文件,換行會加上一個 ^M 字符。因此最後的字符會是隱藏的^M ,在處理Windows

  下面的文本時要特別注意!

  能夠用cat dos_file | tr -d '/r' > unix_file 來刪除^M符號。 ^M==/r

  那麼'^$' 就表示只有行首行尾的空行拉!

  搜索空行

  woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt

  22:

  23:

  woody@xiaoc:~/tmp$

  搜索非空行

  woody@xiaoc:~/tmp$ grep -vn '^$' regular_express.txt

  1:"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 me.

 

  linux關係推薦:

  linux grep怎麼查ip地址

  linux egrep命令在文件或標準輸入中查找模式

  linux fgrep命令查找文件裏符合條件的字符串

  linux rgrep命令遞歸查找文件裏符合條件的字符串

相關文章
相關標籤/搜索