每天一個linux命令(39):grep命令

來源:互聯網
上載者:User


每天一個linux命令(39):grep命令 相關連結:每天一個linux命令(1):ls命令http://www.bkjia.com/os/201210/163049.html;每天一個linux命令(2):cd命令http://www.bkjia.com/os/201210/163050.html;每天一個linux命令(3):pwd命令http://www.bkjia.com/os/201210/163462.html;每天一個linux命令(4):mkdir命令http://www.bkjia.com/os/201210/163463.html;每天一個linux命令(5):rm命令http://www.bkjia.com/os/201210/163662.html;每天一個linux命令(6):rmdir命令http://www.bkjia.com/os/201210/164017.html;每天一個linux命令(7):mv命令http://www.bkjia.com/os/201210/164247.html;每天一個linux命令(8):cp命令http://www.bkjia.com/os/201210/164254.html;每天一個linux命令(9):touch命令http://www.bkjia.com/os/201211/165699.html;每天一個linux命令(10):cat命令http://www.bkjia.com/os/201211/165989.html;每天一個linux命令(11):nl命令http://www.bkjia.com/os/201211/165990.html每天一個linux命令(12):more命令http://www.bkjia.com/os/201211/165994.html每天一個linux命令(13):less命令http://www.bkjia.com/os/201211/165998.html每天一個linux命令(14):head命令http://www.bkjia.com/os/201211/166191.html每天一個linux命令(15):tail命令http://www.bkjia.com/os/201211/168702.html每天一個linux命令(16):which命令http://www.bkjia.com/os/201211/168890.html每天一個linux命令(17):whereis命令http://www.bkjia.com/os/201211/168893.html每天一個linux命令(18):locate命令http://www.bkjia.com/os/201211/168895.html每天一個linux命令(19):find命令概覽http://www.bkjia.com/os/201211/168897.html每天一個linux命令(20):find命令之exechttp://www.bkjia.com/os/201211/168901.html每天一個linux命令(21):find命令之xargshttp://www.bkjia.com/os/201211/168903.html每天一個linux命令(22):find命令的參數詳解http://www.bkjia.com/os/201211/168912.html每天一個linux命令(23):Linux目錄結構http://www.bkjia.com/os/201211/170430.html每天一個linux命令(24):Linux檔案類型與副檔名http://www.bkjia.com/os/201211/170431.html每天一個linux命令(25):linux檔案屬性詳解http://www.bkjia.com/os/201211/170434.html每天一個linux命令(26):用SecureCRT來上傳和下載檔案http://www.bkjia.com/os/201211/172022.html每天一個linux命令(27):linux chmod命令http://www.bkjia.com/os/201211/172028.html每天一個linux命令(28):tar命令http://www.bkjia.com/os/201212/172641.html每天一個linux命令(29):chgrp命令http://www.bkjia.com/os/201212/172983.html每天一個linux命令(30):chown命令http://www.bkjia.com/os/201212/173239.html每天一個linux命令(31):/etc/group檔案詳解http://www.bkjia.com/os/201212/174429.html每天一個linux命令(32):gzip命令http://www.bkjia.com/os/201212/174431.html每天一個linux命令(33):df命令http://www.bkjia.com/os/201212/174434.html每天一個linux命令(34):du命令http://www.bkjia.com/os/201212/174701.html每天一個linux命令(35):ln命令http://www.bkjia.com/os/201212/174993.html每天一個linux命令(36):diff命令http://www.bkjia.com/os/201212/176333.html每天一個linux命令(37):date命令http://www.bkjia.com/os/201212/176335.html每天一個linux命令(38):cal命令http://www.bkjia.com/os/201212/176337.html Linux系統中grep命令是一種強大的文本搜尋工具,它能使用Regex搜尋文本,並把匹 配的行列印出來。grep全稱是Global Regular Expression Print,表示全域Regex版本,它的使用許可權是所有使用者。  www.2cto.com  grep的工作方式是這樣的,它在一個或多個檔案中搜尋字串模板。如果模板包括空格,則必須被引用,模板後的所有字串被看作檔案名稱。搜尋的結果被送到標準輸出,不影響原檔案內容。grep可用於shell指令碼,因為grep通過返回一個狀態值來說明搜尋的狀態,如果模板搜尋成功,則返回0,如果搜尋不成功,則返回1,如果搜尋的檔案不存在,則返回2。我們利用這些傳回值就可進行一些自動化的文本處理工作。1.命令格式:  www.2cto.com  grep [option] pattern file2.命令功能:用於過濾/搜尋的特定字元。可使用Regex能多種命令配合使用,使用上十分靈活。3.命令參數:-a   --text   #不要忽略二進位的資料。   -A<顯示列數>   --after-context=<顯示列數>   #除了顯示符合範本樣式的那一列之外,並顯示該列之後的內容。   -b   --byte-offset   #在顯示符合樣式的那一列之前,標示出該列第一個字元的編號。   -B<顯示列數>   --before-context=<顯示列數>   #除了顯示符合樣式的那一列之外,並顯示該列之前的內容。   -c    --count   #計算符合樣式的列數。   -C<顯示列數>    --context=<顯示列數>或-<顯示列數>   #除了顯示符合樣式的那一列之外,並顯示該列之前後的內容。   -d <動作>      --directories=<動作>   #當指定要尋找的是目錄而非檔案時,必須使用這項參數,否則grep指令將回報資訊並停止動作。   -e<範本樣式>  --regexp=<範本樣式>   #指定字串做為尋找檔案內容的樣式。   -E      --extended-regexp   #將樣式為延伸的普通標記法來使用。   -f<規則檔案>  --file=<規則檔案>   #指定規則檔案,其內容含有一個或多個規則樣式,讓grep尋找符合規則條件的檔案內容,格式為每列一個規則樣式。   -F   --fixed-regexp   #將樣式視為固定字串的列表。   -G   --basic-regexp   #將樣式視為普通的標記法來使用。   -h   --no-filename   #在顯示符合樣式的那一列之前,不標示該列所屬的檔案名稱。   -H   --with-filename   #在顯示符合樣式的那一列之前,表示該列所屬的檔案名稱。   -i    --ignore-case   #忽略字元大小寫差別。   -l    --file-with-matches   #列出檔案內容符合指定的樣式的檔案名稱。   -L   --files-without-match   #列出檔案內容不符合指定的樣式的檔案名稱。   -n   --line-number   #在顯示符合樣式的那一列之前,標示出該列的列數編號。   -q   --quiet或--silent   #不顯示任何資訊。   -r   --recursive   #此參數的效果和指定“-d recurse”參數相同。   -s   --no-messages   #不顯示錯誤資訊。   -v   --revert-match   #顯示不包含匹配文本的所有行。   -V   --version   #顯示版本資訊。   -w   --word-regexp   #只顯示全字元合的列。   -x    --line-regexp   #只顯示全列符合的列。   -y   #此參數的效果和指定“-i”參數相同。  4.規則運算式: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]的另一個寫法。要把它們放到[]號內才能成為Regex,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支援POSIX的字元類。  www.2cto.com  [:alnum:]    #文字數字字元   [:alpha:]    #文字字元   [:digit:]    #數字字元   [:graph:]    #非Null 字元(非空格、控制字元)   [:lower:]    #小寫字元   [:cntrl:]    #控制字元   [:print:]    #非Null 字元(包括空格)   [:punct:]    #標點符號   [:space:]    #所有空白字元(新行,空格,定位字元)   [:upper:]    #大寫字元   [:xdigit:]   #十六進位數字(0-9,a-f,A-F)  5.使用執行個體:執行個體1:尋找指定進程命令:ps -ef|grep svn輸出:[root@localhost ~]# ps -ef|grep svnroot 4943   1      0  Dec05 ?   00:00:00 svnserve -d -r /opt/svndata/grape/root 16867 16838  0 19:53 pts/0    00:00:00 grep svn[root@localhost ~]#說明:第一條記錄是尋找出的進程;第二條結果是grep進程本身,並非真正要找的進程。執行個體2:尋找指定進程個數命令:ps -ef|grep svn -cps -ef|grep -c svn輸出:[root@localhost ~]# ps -ef|grep svn -c2[root@localhost ~]# ps -ef|grep -c svn 2[root@localhost ~]#說明:  www.2cto.com  執行個體3:從檔案中讀取關鍵詞進行搜尋命令:cat test.txt | grep -f test2.txt輸出:[root@localhost test]# cat test.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatRedhatlinuxmint[root@localhost test]# cat test2.txt linuxRedhat[root@localhost test]# cat test.txt | grep -f test2.txthnlinuxubuntu linuxRedhatlinuxmint[root@localhost test]#說明:  www.2cto.com  輸出test.txt檔案中含有從test2.txt檔案中讀取出的關鍵詞的內容行執行個體3:從檔案中讀取關鍵詞進行搜尋 且顯示行號命令:cat test.txt | grep -nf test2.txt輸出:[root@localhost test]# cat test.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatRedhatlinuxmint[root@localhost test]# cat test2.txt linuxRedhat[root@localhost test]# cat test.txt | grep -nf test2.txt1:hnlinux4:ubuntu linux6:Redhat7:linuxmint[root@localhost test]#說明:輸出test.txt檔案中含有從test2.txt檔案中讀取出的關鍵詞的內容行,並顯示每一行的行號執行個體5:從檔案中尋找關鍵詞命令:grep 'linux' test.txt輸出:[root@localhost test]# grep 'linux' test.txt hnlinuxubuntu linuxlinuxmint[root@localhost test]# grep -n 'linux' test.txt 1:hnlinux4:ubuntu linux7:linuxmint[root@localhost test]#說明:執行個體6:從多個檔案中尋找關鍵詞命令:grep 'linux' test.txt test2.txt輸出:[root@localhost test]# grep -n 'linux' test.txt test2.txt test.txt:1:hnlinuxtest.txt:4:ubuntu linuxtest.txt:7:linuxminttest2.txt:1:linux[root@localhost test]# grep 'linux' test.txt test2.txt test.txt:hnlinuxtest.txt:ubuntu linuxtest.txt:linuxminttest2.txt:linux[root@localhost test]#說明:多檔案時,輸出查詢到的資訊內容行時,會把檔案的命名在行最前面輸出並且加上":"作為標示符執行個體7:grep不顯示本身進程命令:ps aux|grep \[s]shps aux | grep ssh | grep -v "grep"輸出:[root@localhost test]# ps aux|grep sshroot   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshdroot  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 root  16901  0.0  0.0  61180   764 pts/0  S+   20:31   0:00 grep ssh[root@localhost test]# ps aux|grep \[s]sh][root@localhost test]# ps aux|grep \[s]shroot   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshdroot  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 [root@localhost test]# ps aux | grep ssh | grep -v "grep"root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshdroot  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0說明:執行個體8:找出已u開頭的行內容命令:cat test.txt |grep ^u輸出:[root@localhost test]# cat test.txt |grep ^uubuntuubuntu linux[root@localhost test]#說明:執行個體9:輸出非u開頭的行內容命令:cat test.txt |grep ^[^u]輸出:[root@localhost test]# cat test.txt |grep ^[^u]hnlinuxpeida.cnblogs.comredhatRedhatlinuxmint[root@localhost test]#說明:執行個體10:輸出以hat結尾的行內容命令:cat test.txt |grep hat$輸出:[root@localhost test]# cat test.txt |grep hat$redhatRedhat[root@localhost test]#說明:執行個體11:命令:輸出:[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0[root@localhost test]#說明:  www.2cto.com  執行個體12:顯示包含ed或者at字元的內容行命令:cat test.txt |grep -E "ed|at"輸出:[root@localhost test]# cat test.txt |grep -E "peida|com"peida.cnblogs.com[root@localhost test]# cat test.txt |grep -E "ed|at"redhatRedhat[root@localhost test]#說明:執行個體13:顯示目前的目錄下面以.txt 結尾的檔案中的所有包含每個字串至少有7個連續小寫字元的字串的行命令:grep '[a-z]\{7\}' *.txt輸出:[root@localhost test]# grep '[a-z]\{7\}' *.txttest.txt:hnlinuxtest.txt:peida.cnblogs.comtest.txt:linuxmint[root@localhost test]#
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.