日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > 服務(wù)器 > Linux常用命令(三十一) - grep

Linux常用命令(三十一) - grep

來源:程序員人生   發(fā)布時間:2014-12-12 08:34:25 閱讀次數(shù):2588次

Linux系統(tǒng)中g(shù)rep命令是1種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹 配的行打印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權(quán)限是所有用戶。


grep的工作方式是這樣的,它在1個或多個文件中搜索字符串模板。如果模板包括空格,則必須被援用,模板后的所有字符串被看做文件名。搜索的結(jié)果被送到標準輸出,不影響原文件內(nèi)容。


grep可用于shell腳本,由于grep通過返回1個狀態(tài)值來講明搜索的狀態(tài),如果模板搜索成功,則返回0,如果搜索不成功,則返回1,如果搜索的文件不存在,則返回2。我們利用這些返回值便可進行1些自動化的文本處理工作。

1.命令格式:

grep [option] pattern file


2.命令功能:

用于過濾/搜索的特定字符。可以使用正則表達式能多種命令配合使用,使用上10分靈活


3.命令參數(shù):

-a   --text   #不要疏忽2進制的數(shù)據(jù)。   

-A<顯示行數(shù)>   --after-context=<顯示行數(shù)>   #除顯示符合范本樣式的那1列以外,并顯示該行以后的內(nèi)容。   

-b   --byte-offset   #在顯示符合樣式的那1行之前,標示出該行第1個字符的編號。   

-B<顯示行數(shù)>   --before-context=<顯示行數(shù)>   #除顯示符合樣式的那1行以外,并顯示該行之前的內(nèi)容。   

-c    --count   #計算符合樣式的列數(shù)。   

-C<顯示行數(shù)>    --context=<顯示行數(shù)>或-<顯示行數(shù)>   #除顯示符合樣式的那1行以外,并顯示該行之前后的內(nèi)容。   

-d <動作>      --directories=<動作>   #當(dāng)指定要查找的是目錄而非文件時,必須使用這項參數(shù),否則grep指令將回報信息并停止動作。   

-e<范本樣式>  --regexp=<范本樣式>   #指定字符串做為查找文件內(nèi)容的樣式。   

-E      --extended-regexp   #將樣式為延伸的普通表示法來使用。   

-f<規(guī)則文件>  --file=<規(guī)則文件>   #指定規(guī)則文件,其內(nèi)容含有1個或多個規(guī)則樣式,讓grep查找符合規(guī)則條件的文件內(nèi)容,格式為每行1個規(guī)則樣式。   

-F   --fixed-regexp   #將樣式視為固定字符串的列表。   

-G   --basic-regexp   #將樣式視為普通的表示法來使用。   

-h   --no-filename   #在顯示符合樣式的那1行之前,不標示該行所屬的文件名稱。   

-H   --with-filename   #在顯示符合樣式的那1行之前,表示該行所屬的文件名稱。   

-i    --ignore-case   #疏忽字符大小寫的差別。   

-l    --file-with-matches   #列出文件內(nèi)容符合指定的樣式的文件名稱。   

-L   --files-without-match   #列出文件內(nèi)容不符合指定的樣式的文件名稱。   

-n   --line-number   #在顯示符合樣式的那1行之前,標示出該行的列數(shù)編號。   

-q   --quiet或--silent   #不顯示任何信息。   

-r   --recursive   #此參數(shù)的效果和指定“-d recurse”參數(shù)相同。   

-s   --no-messages   #不顯示毛病信息。   

-v   --revert-match   #顯示不包括匹配文本的所有行。   

-V   --version   #顯示版本信息。   

-w   --word-regexp   #只顯示全字符合的列。   

-x    --line-regexp   #只顯示全列符合的列。   

-y   #此參數(shù)的效果和指定“-i”參數(shù)相同。


4.規(guī)則表達式

grep的規(guī)則表達式:

^  #錨定行的開始 如:'^grep'匹配所有以grep開頭的行。    

$  #錨定行的結(jié)束 如:'grep$'匹配所有以grep結(jié)尾的行。    

.  #匹配1個非換行符的字符 如:'gr.p'匹配gr后接1個任意字符,然后是p。    

*  #匹配零個或多個先前字符 如:'*grep'匹配所有1個或多個空格后緊跟grep的行。    

.*   #1起用代表任意字符。   

[]   #匹配1個指定范圍內(nèi)的字符,如'[Gg]rep'匹配Grep和grep。    

[^]  #匹配1個不在指定范圍內(nèi)的字符,如:'[^A-FH-Z]rep'匹配不包括A-R和T-Z的1個字母開頭,緊跟rep的行。    

(..)  #標記匹配字符,如'(love)',love被標記為1。    

<      #錨訂單詞的開始,如:'<grep'匹配包括以grep開頭的單詞的行。    

>      #錨訂單詞的結(jié)束,如'grep>'匹配包括以grep結(jié)尾的單詞的行。    

x{m}  #重復(fù)字符x,m次,如:'0{5}'匹配包括5個o的行。    

x{m,}  #重復(fù)字符x,最少m次,如:'o{5,}'匹配最少有5個o的行。    

x{m,n}  #重復(fù)字符x,最少m次,不多于n次,如:'o{5,10}'匹配5-⑴0個o的行。   

w    #匹配文字和數(shù)字字符,也就是[A-Za-z0⑼],如:'Gw*p'匹配以G后跟零個或多個文字或數(shù)字字符,然后是p。   

W    #w的反置情勢,匹配1個或多個非單詞字符,如點號句號等。   

    #單詞鎖定符,如: 'grep'只匹配grep。  


POSIX字符:

為了在不同國家的字符編碼中保持1至,POSIX(The Portable Operating System Interface)增加了特殊的字符類,如[:alnum:]是[A-Za-z0⑼]的另外一個寫法。要把它們放到[]號內(nèi)才能成為正則表達式,如[A- Za-z0⑼]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符類。

[:alnum:]    #文字數(shù)字字符   

[:alpha:]    #文字字符   

[:digit:]    #數(shù)字字符   

[:graph:]    #非空字符(非空格、控制字符)   

[:lower:]    #小寫字符   

[:cntrl:]    #控制字符   

[:print:]    #非空字符(包括空格)   

[:punct:]    #標點符號   

[:space:]    #所有空白字符(新行,空格,制表符)   

[:upper:]    #大寫字符   

[:xdigit:]   #106進制數(shù)字(0⑼,a-f,A-F)  


5.使用實例:

實例1:查找指定進程

命令:ps -ef|grep svn

說明:第1條記錄是查找出的進程;第2條結(jié)果是grep進程本身,并不是真正要找的進程

[root@localhost ~]# ps -ef|grep svn root 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 ~]#

實例2:查找指定進程個數(shù)

命令:

ps -ef|grep svn -c

ps -ef|grep -c svn

[root@localhost ~]# ps -ef|grep svn -c 2 [root@localhost ~]# ps -ef|grep -c svn 2 [root@localhost ~]#

實例3:從文件中讀取關(guān)鍵詞進行搜索

命令:cat test.txt | grep -f test2.txt

說明:輸出test.txt文件中含有從test2.txt文件中讀取出的關(guān)鍵詞的內(nèi)容行

[root@localhost test]# cat test.txt hnlinux peida.cnblogs.com ubuntu ubuntu linux redhat Redhat linuxmint [root@localhost test]# cat test2.txt linux Redhat [root@localhost test]# cat test.txt | grep -f test2.txt hnlinux ubuntu linux Redhat linuxmint [root@localhost test]#

實例4:從文件中讀取關(guān)鍵詞進行搜索 且顯示行號

命令:cat test.txt | grep -nf test2.txt

說明:輸出test.txt文件中含有從test2.txt文件中讀取出的關(guān)鍵詞的內(nèi)容行,并顯示每行的行號

[root@localhost test]# cat test.txt hnlinux peida.cnblogs.com ubuntu ubuntu linux redhat Redhat linuxmint [root@localhost test]# cat test2.txt linux Redhat [root@localhost test]# cat test.txt | grep -nf test2.txt 1:hnlinux 4:ubuntu linux 6:Redhat 7:linuxmint [root@localhost test]#

實例5:從文件中查找關(guān)鍵詞

命令:grep 'linux' test.txt

[root@localhost test]# grep 'linux' test.txt hnlinux ubuntu linux linuxmint [root@localhost test]# grep -n 'linux' test.txt 1:hnlinux 4:ubuntu linux 7:linuxmint [root@localhost test]#


實例6從多個文件中查找關(guān)鍵詞

命令:grep 'linux' test.txt test2.txt

說明:多文件時,輸出查詢到的信息內(nèi)容行時,會把文件的命名在行最前面輸出并且加上":"作為標示符

[root@localhost test]# grep -n 'linux' test.txt test2.txt test.txt:1:hnlinux test.txt:4:ubuntu linux test.txt:7:linuxmint test2.txt:1:linux [root@localhost test]# grep 'linux' test.txt test2.txt test.txt:hnlinux test.txt:ubuntu linux test.txt:linuxmint test2.txt:linux [root@localhost test]#

實例7:grep不顯示本身進程

命令:

ps aux|grep [s]sh

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

[root@localhost test]# ps aux|grep ssh root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd root 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]sh root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd root 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/sshd root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0

實例8:找出已u開頭的行內(nèi)容

命令:cat test.txt |grep ^u

[root@localhost test]# cat test.txt |grep ^u ubuntu ubuntu linux [root@localhost test]#

實例9:輸出非u開頭的行內(nèi)容

命令:cat test.txt |grep ^[^u]

[root@localhost test]# cat test.txt |grep ^[^u] hnlinux peida.cnblogs.com redhat Redhat linuxmint [root@localhost test]#

實例10:輸出以hat結(jié)尾的行內(nèi)容

命令:cat test.txt |grep hat$

[root@localhost test]# cat test.txt |grep hat$ redhat Redhat [root@localhost test]#

實例101:顯示包括ed或at字符的內(nèi)容行

命令: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" redhat Redhat [root@localhost test]#

實例102:顯示當(dāng)前目錄下面以.txt 結(jié)尾的文件中的所有包括每一個字符串最少有7個連續(xù)小寫字符的字符串的行

命令:grep '[a-z]{7}' *.txt

[root@localhost test]# grep '[a-z]{7}' *.txt test.txt:hnlinux test.txt:peida.cnblogs.com test.txt:linuxmint [root@localhost test]#

原文地址:http://www.cnblogs.com/peida/archive/2012/12/17/2821195.html



生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 国产一卡久久电影永久 | 欧美午夜在线 | 日韩精品免费观看 | a级片免费网站 | 国产精品国产三级国产aⅴ9色 | 狼人综合网 | 久久国产精品久久 | 精品一区二区三区在线播放 | 国产欧美日韩综合精品一区二区 | 久久久久国产一区二区三区 | 最新国产在线 | 国产美女久久久 | 久久小草 | 91免费视频观看 | 日韩在线一区二区 | 在线1区 | 精品久久久久久久久久久院品网 | 中文字幕在线观看第一页 | 毛片在线视频 | 久久九九免费视频 | 夜夜骑狠狠干 | 久久这里只有精品首页 | 99精彩视频 | 欧美成人高清 | 欧美黄色aa | 久久网站免费观看 | 97成人在线视频 | 精品一区二区久久久久久久网站 | 中文字幕在线观看一区二区三区 | 中文字幕成人网 | 成人aa| а_天堂中文最新版地址 | 国产v亚洲v天堂无码 | 久久国产亚洲精品 | 看中国毛片 | 成人区精品一区二区 | 中文字幕免费在线 | 日韩高清在线 | 一区二区在线免费 | 亚洲精品国产一区 | 色综合狠狠干 |