Linux:grep命令

Global Regular Expression Print

使用实例:

实例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]#

说明:

实例6:从多个文件中查找关键词

命令:

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
[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开头的行内容

命令:

cat test.txt |grep ^u

输出:

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

说明:

实例9:输出非u开头的行内容

命令:

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结尾的行内容

命令:

cat test.txt |grep hat$

输出:

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

说明:

实例11:输出ip地址

命令:

     ifconfig eth0|grep -E "([0-9]{1,3}.){3}[0-9]"

输出:

[root@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
[root@localhost test]#

说明:

实例12:显示包含ed或者at字符的内容行

命令:

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

说明:

实例13:显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行

命令:

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

实例14:日志文件过大,不好查看,我们要从中查看自己想要的内容,或者得到同一类数据,比如说没有404日志信息的

命令:

grep '.' access1.log|grep -Ev '404' > access2.log

grep '.' access1.log|grep -Ev '(404|/photo/|/css/)' > access2.log

grep '.' access1.log|grep -E '404' > access2.log

输出:

[root@localhost test]# grep “.”access1.log|grep -Ev “404” > access2.log

说明:上面3句命令前面两句是在当前目录下对access1.log文件进行查找,找到那些不包含404的行,把它们放到access2.log中,后面去掉’v’,即是把有404的行放入access2.log

 

grep -w与grep -e的区别

 

 

    grep -w

    -w
    Does a word search.
    grep -w用于字符串精确匹配
    若文件中的内容包括如下:
    262 a3
    262
    26
    如果 grep26file,结果是三行全部都被显示
    若要精确匹配26所在行
    使用grep -w26file

    grep -e
    显示文件中符合条件的字符
    查找当前目录下所有文件中包含字符串”Linux”的文件,会将含有Linux字符串的所有文件匹配出来。

 

 

原文地址:https://www.cnblogs.com/xwb583312435/p/9018091.html