系統(tǒng)之家 - 系統(tǒng)光盤下載網(wǎng)站!

當(dāng)前位置:系統(tǒng)之家 > 系統(tǒng)教程 > Linux使用grep命令搜索文件

Linux如何使用grep命令搜索文本文件

時(shí)間:2014-12-30 16:00:08 作者:qipeng 來源:系統(tǒng)之家 1. 掃描二維碼隨時(shí)看資訊 2. 請(qǐng)使用手機(jī)瀏覽器訪問: https://m.xitongzhijia.net/xtjc/20141230/33749.html 手機(jī)查看 評(píng)論

  Linux系統(tǒng)中如果你想要搜索文本文件的話,可以使用grep命令,通過grep命令,你可以搜索相關(guān)關(guān)鍵詞文件,也可對(duì)符合條件的文本文件進(jìn)行篩選,是個(gè)比較常用的命令,下面小編就給大家介紹下Linux使用grep命令搜索文本文件的方法。

 Linux如何使用grep命令搜索文本文件

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

  grep可用于shell腳本,因?yàn)間rep通過返回一個(gè)狀態(tài)值來說明搜索的狀態(tài),如果模板搜索成功,則返回0,如果搜索不成功,則返回1,如果搜索的文件不存在,則返回2。我們利用這些返回值就可進(jìn)行一些自動(dòng)化的文本處理工作。

  1.命令格式:

  代碼如下:

  grep [option] pattern file

  2.命令功能:

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

  3.命令參數(shù):

  -a --text #不要忽略二進(jìn)制的數(shù)據(jù)。

  -A《顯示行數(shù)》 --after-context=《顯示行數(shù)》 #除了顯示符合范本樣式的那一列之外,并顯示該行之后的內(nèi)容。

  -b --byte-offset #在顯示符合樣式的那一行之前,標(biāo)示出該行第一個(gè)字符的編號(hào)。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  -s --no-messages #不顯示錯(cuò)誤信息。

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

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

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

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

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

  4.規(guī)則表達(dá)式:

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

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

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

  。 #匹配一個(gè)非換行符的字符 如:‘gr.p’匹配gr后接一個(gè)任意字符,然后是p。

  * #匹配零個(gè)或多個(gè)先前字符 如:‘*grep’匹配所有一個(gè)或多個(gè)空格后緊跟grep的行。

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

 。郏 #匹配一個(gè)指定范圍內(nèi)的字符,如‘[Gg]rep’匹配Grep和grep。

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

  \(。。\) #標(biāo)記匹配字符,如‘\(love\)’,love被標(biāo)記為1。

  \《 #錨定單詞的開始,如:‘\《grep’匹配包含以grep開頭的單詞的行。

  \》 #錨定單詞的結(jié)束,如‘grep\》’匹配包含以grep結(jié)尾的單詞的行。

  x\{m\} #重復(fù)字符x,m次,如:‘0\{5\}’匹配包含5個(gè)o的行。

  x\{m,\} #重復(fù)字符x,至少m次,如:‘o\{5,\}’匹配至少有5個(gè)o的行。

  x\{m,n\} #重復(fù)字符x,至少m次,不多于n次,如:‘o\{5,10\}’匹配5--10個(gè)o的行。

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

  \W #\w的反置形式,匹配一個(gè)或多個(gè)非單詞字符,如點(diǎn)號(hào)句號(hào)等。

  \b #單詞鎖定符,如: ‘\bgrep\b’只匹配grep。

  POSIX字符:

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

 。郏篴lnum:] #文字?jǐn)?shù)字字符

  [:alpha:] #文字字符

 。郏篸igit:] #數(shù)字字符

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

 。郏簂ower:] #小寫字符

  [:cntrl:] #控制字符

 。郏簆rint:] #非空字符(包括空格)

  [:punct:] #標(biāo)點(diǎn)符號(hào)

 。郏簊pace:] #所有空白字符(新行,空格,制表符)

 。郏簎pper:] #大寫字符

  [:xdigit:] #十六進(jìn)制數(shù)字(0-9,a-f,A-F)

  5.使用實(shí)例:

  實(shí)例1:查找指定進(jìn)程

  命令:ps -ef|grep svn

  輸出:

  代碼如下:

 。踨oot@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

 。踨oot@localhost ~]#

  說明:第一條記錄是查找出的進(jìn)程;第二條結(jié)果是grep進(jìn)程本身,并非真正要找的進(jìn)程。

  實(shí)例2:查找指定進(jìn)程個(gè)數(shù)

  命令:

  代碼如下:

  ps -ef|grep svn -c

  ps -ef|grep -c svn

  輸出:

  代碼如下:

 。踨oot@localhost ~]# ps -ef|grep svn -c

  2

 。踨oot@localhost ~]# ps -ef|grep -c svn

  2

  [root@localhost ~]#

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

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

  輸出:

  代碼如下:

 。踨oot@localhost test]# cat test.txt

  hnlinux

  peida.cnblogs.com

  ubuntu

  ubuntu linux

  redhat

  Redhat

  linuxmint

 。踨oot@localhost test]# cat test2.txt

  linux

  Redhat

  [root@localhost test]# cat test.txt | grep -f test2.txt

  hnlinux

  ubuntu linux

  Redhat

  linuxmint

 。踨oot@localhost test]#

  說明:

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

  實(shí)例3:從文件中讀取關(guān)鍵詞進(jìn)行搜索 且顯示行號(hào)

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

  輸出:

  代碼如下:

 。踨oot@localhost test]# cat test.txt

  hnlinux

  peida.cnblogs.com

  ubuntu

  ubuntu linux

  redhat

  Redhat

  linuxmint

  [root@localhost test]# cat test2.txt

  linux

  Redhat

 。踨oot@localhost test]# cat test.txt | grep -nf test2.txt

  1:hnlinux

  4:ubuntu linux

  6:Redhat

  7:linuxmint

 。踨oot@localhost test]#

  說明:

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

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

  命令:grep ‘linux’ test.txt

  輸出:

  代碼如下:

 。踨oot@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

 。踨oot@localhost test]#

  實(shí)例6:從多個(gè)文件中查找關(guān)鍵詞

  命令:grep ‘linux’ test.txt test2.txt

  輸出:

  代碼如下:

 。踨oot@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

 。踨oot@localhost test]#

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

  實(shí)例7:grep不顯示本身進(jìn)程

  命令:

  代碼如下:

  ps aux|grep \[s]sh

  ps aux | grep ssh | grep -v “grep”

  輸出:

  代碼如下:

 。踨oot@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: 《a href=“mailto:root@pts/0”》root@pts/0《/a》

  root 16901 0.0 0.0 61180 764 pts/0 S+ 20:31 0:00 grep ssh

 。踨oot@localhost test]# ps aux|grep \[s]sh]

 。踨oot@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: 《a href=“mailto:root@pts/0”》root@pts/0《/a》

 。踨oot@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: 《a href=“mailto:root@pts/0”》root@pts/0《/a》

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

  命令:cat test.txt |grep ^u

  輸出:

  代碼如下:

 。踨oot@localhost test]# cat test.txt |grep ^u

  ubuntu

  ubuntu linux

  [root@localhost test]#

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

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

  輸出:

  代碼如下:

 。踨oot@localhost test]# cat test.txt |grep ^[^u]

  hnlinux

  peida.cnblogs.com

  redhat

  Redhat

  linuxmint

  [root@localhost test]#

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

  命令:cat test.txt |grep hat$

  輸出:

  代碼如下:

 。踨oot@localhost test]# cat test.txt |grep hat$

  redhat

  Redhat

 。踨oot@localhost test]#

  實(shí)例11:

  命令: ifconfig eth0|grep “[0-9]\{1,3\}\。[0-9]\{1,3\}\。[0-9]\{1,3\}\。[0-9]\{1,3\}”

  輸出:

  代碼如下:

 。踨oot@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

 。踨oot@localhost test]#

  實(shí)例12:顯示包含ed或者at字符的內(nèi)容行

  命令:cat test.txt |grep -E “ed|at”

  輸出:

  代碼如下:

 。踨oot@localhost test]# cat test.txt |grep -E “peida|com”

  peida.cnblogs.com

 。踨oot@localhost test]# cat test.txt |grep -E “ed|at”

  redhat

  Redhat

 。踨oot@localhost test]#

  實(shí)例13:顯示當(dāng)前目錄下面以.txt 結(jié)尾的文件中的所有包含每個(gè)字符串至少有7個(gè)連續(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

 。踨oot@localhost test]#

  上面就是Linux使用grep命令搜索文本文件的方法介紹了,除了文本的搜索,還能從文件中查找關(guān)鍵詞,查找指定進(jìn)程,本文已通過各種實(shí)例進(jìn)行介紹,希望你有個(gè)了解。

標(biāo)簽 命令 grep

發(fā)表評(píng)論

0

沒有更多評(píng)論了

評(píng)論就這些咯,讓大家也知道你的獨(dú)特見解

立即評(píng)論

以上留言僅代表用戶個(gè)人觀點(diǎn),不代表系統(tǒng)之家立場(chǎng)

其他版本軟件

熱門教程

人氣教程排行

Linux系統(tǒng)推薦

官方交流群 軟件收錄