`
天梯梦
  • 浏览: 13629639 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

linux命令之grep 命令

 
阅读更多

 

Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

 

grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名。搜索的结果被送到标准输出,不影响原文件内容。

 

grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。

 

1.命令格式:

grep [option] pattern file

 

2.命令功能:

用于过滤/搜索的特定字符。可使用正则表达式能多种命令配合使用,使用上十分灵活

 

3.命令参数:

grep --help
匹配模式选择:
 -E, --extended-regexp     扩展正则表达式egrep
 -F, --fixed-strings       一个换行符分隔的字符串的集合fgrep
 -G, --basic-regexp        基本正则
 -P, --perl-regexp         调用的perl正则
 -e, --regexp=PATTERN      后面根正则模式,默认无
 -f, --file=FILE           从文件中获得匹配模式
 -i, --ignore-case         不区分大小写
 -w, --word-regexp         匹配整个单词
 -x, --line-regexp         匹配整行
 -z, --null-data           一个 0 字节的数据行,但不是空行

杂项:
 -s, --no-messages         不显示错误信息
 -v, --invert-match        显示不匹配的行
 -V, --version             显示版本号
 --help                    显示帮助信息
 --mmap                use memory-mapped input if possible

输入控制:
 -m, --max-count=NUM       匹配的最大数
 -b, --byte-offset         打印匹配行前面打印该行所在的块号码。
 -n, --line-number         显示的加上匹配所在的行号
 --line-buffered           刷新输出每一行
 -H, --with-filename       当搜索多个文件时,显示匹配文件名前缀
 -h, --no-filename         当搜索多个文件时,不显示匹配文件名前缀
 --label=LABEL            print LABEL as filename for standard input
 -o, --only-matching       只显示一行中匹配PATTERN 的部分
 -q, --quiet, --silent      不显示任何东西
 --binary-files=TYPE   假定二进制文件的TYPE 类型;
                                      TYPE 可以是`binary', `text', 或`without-match'
 -a, --text                匹配二进制的东西
 -I                        不匹配二进制的东西
 -d, --directories=ACTION  目录操作,读取,递归,跳过
 -D, --devices=ACTION      设置对设备,FIFO,管道的操作,读取,跳过
 -R, -r, --recursive       递归调用
 --include=PATTERN     只查找匹配FILE_PATTERN 的文件
 --exclude=PATTERN     跳过匹配FILE_PATTERN 的文件和目录
 --exclude-from=FILE   跳过所有除FILE 以外的文件
 -L, --files-without-match 匹配多个文件时,显示不匹配的文件名
 -l, --files-with-matches  匹配多个文件时,显示匹配的文件名
 -c, --count               显示匹配了多少次
 -Z, --null                在FILE 文件最后打印空字符

文件控制:
 -B, --before-context=NUM  打印匹配本身以及前面的几个行由NUM控制
 -A, --after-context=NUM   打印匹配本身以及随后的几个行由NUM控制
 -C, --context=NUM         打印匹配本身以及随后,前面的几个行由NUM控制
 -NUM                      根-C的用法一样的
 --color[=WHEN],
 --colour[=WHEN]       使用标志高亮匹配字串;
 
 -U, --binary               使用标志高亮匹配字串;
 -u, --unix-byte-offsets   当CR 字符不存在,报告字节偏移(MSDOS 模式)  

 

4.则表达式

 

grep的规则表达式:

^  #锚定行的开始 如:'^grep'匹配所有以grep开头的行。    

$  #锚定行的结束 如:'grep$'匹配所有以grep结尾的行。    

.  #匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。    

*  #匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。    

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

[]   #匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。    

[^]  #匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。    

\(..\)  #标记匹配字符,如'\(love\)',love被标记为1。    

\<      #锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。    

\>      #锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。    

x\{m\}  #重复字符x,m次,如:'0\{5\}'匹配包含5个o的行。    

x\{m,\}  #重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。    

x\{m,n\}  #重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。   

\w    #匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。   

\W    #\w的反置形式,匹配一个或多个非单词字符,如点号句号等。   

\b    #单词锁定符,如: '\bgrep\b'只匹配grep。  

 

POSIX字符:

为 了在不同国家的字符编码中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符 类,如[:alnum:]是[A-Za-z0-9]的另一个写法。要把它们放到[]号内才能成为正则表达式,如[A- Za-z0-9]或 [[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符类。

[:alnum:]    #文字数字字符   

[:alpha:]    #文字字符   

[:digit:]    #数字字符   

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

[:lower:]    #小写字符   

[:cntrl:]    #控制字符   

[:print:]    #非空字符(包括空格)   

[:punct:]    #标点符号   

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

[:upper:]    #大写字符   

[:xdigit:]   #十六进制数字(0-9,a-f,A-F)  

 

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

说明:

 

实例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:

命令:

输出:

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

说明:

 

来源: http://www.cnblogs.com/peida/archive/2012/12/17/2821195.html

 

 

测试文件

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa
DADddd:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po

 

a,匹配含有root的行

[root@krlcgcms01 test]# grep root test  
root:x:0:0:root:/root:/bin/bash  

 

b,匹配以root开头或者以zhang开头的行,注意反斜杠

[root@krlcgcms01 test]# cat test |grep '^\(root\|zhang\)'  
root:x:0:0:root:/root:/bin/bash  
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  

 

c,匹配以root开头或者以zhang开头的行,注意反斜杠,根上面一个例子一样,-e默认是省去的

[root@krlcgcms01 test]# cat test |grep -e '^\(root\|zhang\)'  
root:x:0:0:root:/root:/bin/bash  
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  

 

d,匹配以zhang开头,只含有字母

[root@krlcgcms01 test]# echo 'zhangying' |grep '^zhang[a-z]*$'  
zhangying  

 

e,匹配以bin开头的行,用的egrep,在这里可以换成-F,-G

[root@krlcgcms01 test]# cat test |grep -E '^bin'  
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa 

 

f,在匹配的行前面加上该行在文件中,或者输出中所在的行号

[root@krlcgcms01 test]# cat test|grep -n zhangy  
7:zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  
13:ba:x:1002:1002::/home/zhangy:/bin/bash  
15:@zhangying:*:1004:1004::/home/test:/bin/bash  

 

g,不匹配以bin开头的行,并显示行号

[root@krlcgcms01 test]# cat test|grep -nv '^bin'  
root:x:0:0:root:/root:/bin/bash
DADddd:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po

 

h,显示匹配的个数,不显示内容

[root@krlcgcms01 test]#  cat test|grep -c zhang  
3  

 

i,匹配system,没有加-i没有匹配到东西。

[root@krlcgcms01 test]# grep  system test  
[root@krlcgcms01 test]# grep -ni  system test  
9:dbus:x:81:81:System message bus:/:/bin/false  

 

j,匹配zhan没有匹配到东西,匹配zhangy能匹配到,因为在test文件中,有zhangy这个单词

[root@krlcgcms01 test]#  cat test|grep -w zhan  
[root@krlcgcms01 test]#  cat test|grep -w zhangy  
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  
ba:x:1002:1002::/home/zhangy:/bin/bash  

 

k,在这里-x后面东西,和输出中的整行相同时,才会输出

[root@krlcgcms01 test]# echo "aaaaaa" |grep -x aaa  
[root@krlcgcms01 test]# echo "aaaa" |grep -x aaaa  
aaaa  

 

l,最多只匹配一次,如果把-m 1去掉的话,会有三个

[root@krlcgcms01 test]# cat test |grep -m 1 zhang  
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  

 

m,匹配行的前面显示块号,这个块号是干什么的,不知道,有谁知道可否告诉我一下

[apacheuser@krlcgcms01 test]$ cat test |grep -b zha  
241:zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash  
480:ba:x:1002:1002::/home/zhangy:/bin/bash  
558:@zhangying:*:1004:1004::/home/test:/bin/bash  

 

n,多文件匹配时,在匹配的行前面加上文件名

[apacheuser@krlcgcms01 test]$ grep -H 'root' test test2 testbak  
test:root:x:0:0:root:/root:/bin/bash  
test2:root  
testbak:root:x:0:0:root:/root:/bin/bash  

 

o,多文件匹配时,在匹配的行前面不加上文件名

[apacheuser@krlcgcms01 test]$ grep -h 'root' test test2 testbak  
root:x:0:0:root:/root:/bin/bash  
root  
root:x:0:0:root:/root:/bin/bash  

 

p,多文件匹配时,显示匹配文件的文件名

[apacheuser@krlcgcms01 test]$ grep -l 'root' test test2 testbak DAta  
test  
test2  
testbak  

 

q,没有-o时,有一行匹配,这一行里面有3个root,加上-o后,这个3个root就出来了

[apacheuser@krlcgcms01 test]$ grep  'root' test  
root:x:0:0:root:/root:/bin/bash  
[apacheuser@krlcgcms01 test]$ grep -o 'root' test  
root  
root  
root  

 

r,递归显示匹配的内容,在test目录下面建个mytest目录,copy test目录下面的test文件到mytest下面,能看到上面的结果

[root@krlcgcms01 test]# grep test -R /tmp/test/mytest  
/tmp/test/mytest/test:test:x:1003:1003::/home/test:/bin/bash  
/tmp/test/mytest/test:@zhangying:*:1004:1004::/home/test:/bin/bash  

 

s,显示匹配root后面的3行

[root@krlcgcms01 test]# cat test |grep -A 3 root  
root:x:0:0:root:/root:/bin/bash  
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa  
daemon:x:2:2:daemon:/sbin:/bin/false  
mail:x:8:12:mail:/var/spool/mail:/bin/false  

 

来源:http://linux.51yip.com/search/grep

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics