龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 操作系统 > LINUX系统 >

linux命令大全之grep命令详解(文本搜索工具)(2)

时间:2014-05-19 15:24来源:网络整理 作者:网络 点击:
分享到:
5.使用实例: 实例1:查找指定进程 命令:ps -ef|grep svn 输出: 代码如下: [root@localhost ~]# ps -ef|grep svn root 4943 1 0 Dec05 ? 00:00:00 svnserve -d -r /opt/svndata/grape/

5.使用实例:
实例1:查找指定进程
命令:ps -ef|grep svn
输出:

代码如下:

[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 ~]#

说明:第一条记录是查找出的进程;第二条结果是grep进程本身,并非真正要找的进程。

实例2:查找指定进程个数
命令:

代码如下:

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:从文件中读取关键词进行搜索
命令:cat test.txt | grep -f test2.txt
输出:

代码如下:

[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]#

说明:
输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行

实例3:从文件中读取关键词进行搜索 且显示行号
命令:cat test.txt | grep -nf test2.txt
输出:

代码如下:

[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]#

说明:
输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行,并显示每一行的行号

实例5:从文件中查找关键词
命令: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]#

精彩图集

赞助商链接