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

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

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

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

  輸出:

  代碼如下:

  [root@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 -nf test2.txt

  1:hnlinux

  4:ubuntu linux

  6:Redhat

  7:linuxmint

 。踨oot@localhost test]#

  說明:

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

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

  命令:grep ‘linux’ test.txt

  輸出:

  代碼如下:

 。踨oot@localhost test]# grep ‘linux’ test.txt

  hnlinux

  ubuntu linux

  linuxmint

 。踨oot@localhost test]# grep -n ‘linux’ test.txt

  1:hnlinux

  4:ubuntu linux

  7:linuxmint

 。踨oot@localhost test]#

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

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

  輸出:

  代碼如下:

  [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

 。踨oot@localhost test]# grep ‘linux’ test.txt test2.txt

  test.txt:hnlinux

  test.txt:ubuntu linux

  test.txt:linuxmint

  test2.txt:linux

 。踨oot@localhost test]#

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

  實例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: 《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》

發(fā)表評論

0

沒有更多評論了

評論就這些咯,讓大家也知道你的獨特見解

立即評論

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

其他版本軟件

熱門教程

人氣教程排行

Linux系統(tǒng)推薦

官方交流群 軟件收錄