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

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

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

時(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)論

  POSIX字符:

  為了在不同國(guó)家的字符編碼中保持一至,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ù)字字符

 。郏篴lpha:] #文字字符

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

 。郏篻raph:] #非空字符(非空格、控制字符)

 。郏簂ower:] #小寫字符

 。郏篶ntrl:] #控制字符

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

 。郏簆unct:] #標(biāo)點(diǎn)符號(hào)

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

 。郏簎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

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

標(biāo)簽 命令 grep

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

0

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

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

立即評(píng)論

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

其他版本軟件

熱門教程

人氣教程排行

Linux系統(tǒng)推薦

官方交流群 軟件收錄