linux grep命令使用详解

grep是我们最常用的命令之一,但是正真用的熟的不多,把基础命令记牢固,能帮我们节约很多时间

grep的option

$cat a.txt
a
bb
ccc
dddd
1a
2b
3d
4a
5a

-A 1 表示找到所有匹配行,并显示所有匹配行后的一行
在错误日志查找时还是很有用

$ grep -A 1 b a.txt
bb                //找到b了
ccc              //显示bb这一行的后面一行
--
2b
3d

-B 1 表示找到所有匹配行,并显示所有匹配行的前面一行

$ grep -B 1 b a.txt
a
bb             //匹配到b了,把bb这一行到前一行也显示出来
--
1a
2b

-C 1表示找到所有匹配行,并显示所有匹配行的前一行,后一行

$ grep -C 1 b a.txt
a
bb             //匹配到b了,把a,ccc前后一行也显示出来
ccc
--
--
1a
2b
3d

-a 表示把所有文件当作ASCII文件来处理 搜索二进制文件
//a.pcap是一个tcpdump生成的文件,为binary文件,使用grep不能搜索


$ grep "HOST" a.pcap
Binary file a.pcap matches

$ grep -a "HOST" a.pcap
HOST: 239.255.255.250:1900
HOST: 239.255.255.250:1900

-b 表示显示match的字符串在文件中的offset

$grep -b  a a.txt
0:a
14:1a        //表示这一行的a在文件中的offset为14
23:4a
26:5a

-c 显示有多少行match

$grep -c a a.tx
4                 

–color 是把match的字符用不同颜色标示出来

#grep --color a a.txt
a
1a
4a
5a

-e 后面跟一个正则表达式,指定多个正则表达式的时候很有用

$grep -e [a-c]  -e [0-2] a.txt      //匹配a,b,c   或者0,1,2 
a
bb
ccc
1a
2b
4a
5a
2

-f可以指定pattern在我们的文件中 pattern文件中的每一行都会来进行匹配

$ cat pattern.txt
a
2

$ cat a.txt
a
bb
ccc
dddd
1a
2b
3d
4a
5a
2

$grep -f pattern.txt  a.txt
a
1a
2b
4a
5a
2

-m 最多匹配几个后,就停止,这样速度会比较快

$ grep -m 2 a a.txt
a
1a

-n 匹配之后,在前面打印行号,这个还是有用的

$ grep -n a a.txt
1:a
5:1a
8:4a
9:5a

-o 只打印匹配的内容

$ grep -o a a.txt
a
a
a
a

-R 搜索子目录
$ll
total 208
-rw-r–r– 1 * * 91868 5 1 23:05 a.pcap
-rw-r–r– 1 * * 31 5 2 22:39 a.txt
-rw-r–r– 1 * * 4 5 1 23:18 b.txt
drwxr-xr-x 3 * * 102 5 4 18:58 dir1 //这是一个目录
-rw-r–r– 1 * * 4 5 4 18:49 pattern.txt

//只能搜索当前目录

$grep  a *
Binary file a.pcap matches
a.txt:a
a.txt:1a
a.txt:4a
a.txt:5a
b.txt:aaa
grep: dir1: Is a directory      //碰到一个目录了
pattern.txt:a
$grep -R a *           //搜索子目录
Binary file a.pcap matches
a.txt:a
a.txt:1a
a.txt:4a
a.txt:5a
b.txt:aaa
dir1/b.txt:a                  //这个目录也找到了
pattern.txt:a

-v invert查找 下面为在a.txt中,查找不包含”a”的行 这个命令会经常使用

$ grep -v a a.txt
bb
ccc
dddd
2b
3d
2

-w 查找一个词,

#cat word.txt
it is a good day   //分成五个词,分别是it , is, a, good, day
itis a goodday     //分成三个词,分别是itis, a,  goodday


$ grep -w it word.txt   
it is a good day

$ grep -w good word.txt
it is a good day

grep 正则表达式

^pattern 表示开头处开始匹配

$echo "it is "|grep "^is"      //因为is不在行的开始处,所以没有匹配上

$echo "it is "|grep "^i"
it is


pattern$ 从结尾处匹配         
$ echo "it is "|grep "it$"          //这个匹配不上


$ echo "it is "|grep "is$"          //能匹配上
it is

. 为匹配任意单个字符

$ echo "it is" |grep "."
it is

$echo "it is" |grep "a."


".*"  匹配任意长度的任意字符
$ echo "it is" |grep ".*"
it is

$ echo "#" |grep ".*"        //匹配任意字符,所以#也可以匹配
#

“r.*h”
//匹配一个字符串,其中有r,有h,且r在前,h在后,且r和h之间可以间隔任意长度的任意字符串,当然也可以没有间隔

$ echo "redhat" |grep "r.*h"
redhat

$ echo "rh" |grep "r.*h"
rh

$ echo "redhat" |grep "r.*b"


"^r.*h$"   

//匹配一个字符串,其中r是第一个字符,h是最后一个字符,且r和h之间可以间隔任意长度的任意字符串,当然也可以没有间隔

$ echo "rh" |grep "^r.*h$"
rh

$ echo "redh" |grep "^r.*h$"
redh

$ echo "redhat" |grep "^r.*h$"       //这里匹配不上

匹配指定范围内的任意字符 [e] 匹配任意单个字符
$ echo “redh” |grep “[e]”
redh

$ echo “redh” |grep “[s]”

匹配指定范围内的任意字符 [eh] 匹配e或h

$ echo "redh" |grep "[eh]"
redh

$ echo "redh" |grep "[sh]"
redh

匹配指定范围内的任意字符 [a-z],[1-3a-b] [1-3a-bX-Z]

$ echo "redh" |grep "[a-d]"
redh

$ echo "redh" |grep "[d-f]"
redh
$ echo "redh" |grep "[d-a]"
grep: invalid character range

$ echo "redh" |grep "[f-f]"      

$ echo "redh" |grep "[a-z]"
redh

$ echo "redX" |grep "[x-x]"

$ echo "redX" |grep "[X-X]"
redX

$ echo "redX" |grep "[a-Z]"
grep: invalid character range


$ echo "redX" |grep "[x-X]"       //不要欺负计算机
grep: invalid character range


$ echo "16" |grep "[1-2]"
16


$ echo "16" |grep "[5-8]"
16

$ echo "16" |grep "[1-9a-z]"
16

$ echo "b" |grep "[1-9a-z]"
b

$ echo "1xX" |grep "[1-1x-zX-X]"
1xX

//[^] 匹配指定范围外的字符

$ echo "1" |grep "[^1-3]"      //匹配非[1-3]的,不能匹配上
$ echo "2" |grep "[^1-3]"      //匹配非[1-3]的,不能匹配上
$ echo "3" |grep "[^1-3]"      //匹配非[1-3]的,不能匹配上
$ echo "4" |grep "[^1-3]"      //匹配非[1-3]的,能匹配上
4

$ echo "a" |grep "[^1-3]"      //匹配非[1-3]的
a

$echo "a" |grep "[^1-3a-b]"

$echo "a" |grep "[^1-3a-b]"    //匹配非[1-3],非[a-b]的 ,匹配不上

$echo "c" |grep "[^1-3a-b]"    //匹配非[1-3],非[a-b]的 ,匹配不上
c

$echo "4" |grep "[^1-3a-b]"   //匹配非[1-3],非[a-b]的 ,能匹配上
4

[[:space:]] 匹配空白字符

$ echo "a"|grep "[[:space:]]"

$ echo "a b"|grep "[[:space:]]"
a b

\? 表示匹配前面的字符串0次或1次

$ echo "z"|grep "z\(a\?\)"
z

$ echo "za"|grep "z\(a\?\)"
za

$ echo "ab"|grep "z\(a\?\)"    //不匹配

{m,n} 匹配前面的字符串m到n次

$ echo "aa"|grep "a\{1,3\}"       //匹配a,aa,aaa
aa

$ echo "aa"|grep "a\{2,3\}"
aa

$ echo "aa"|grep "a\{3,4\}"      //不匹配

{2,} 匹配前面的字符串至少两次

$ echo "aa"|grep "a\{1,\}"
aa

$ echo "aa"|grep "a\{2,\}"
aa

$ echo "aa"|grep "a\{3,\}"    //不匹配

// \

$ echo "redhat"|grep "\"
redhat

$ echo "redhat"|grep "\"
redhat

$ echo "zredhat"|grep "\"          //不匹配
$ echo "redhat"|grep "\"          //不匹配
$ echo "redhat"|grep "^r[a-z]*t$"            //相当于这个
redhat

//() 用来进行分组, 下面这个就是匹配带有re和rea的

$ echo "redhat"|grep "\(re\)\(a\?\)"
redhat

$ echo "zedhat"|grep "\(re\)\(a\?\)"       //匹配不上

+ 表示匹配前面的字符串一次或多次

$ echo "zredhat"|grep "\(re\)\(d\+\)"      //匹配red,redd,reddd,redddd,...
zredhat

$ echo "zrehat"|grep "\(re\)\(d\+\)"      //匹配不上

| 表示或的关系

$ echo "d"|grep "\(re\)\|\(d\+\)"       //匹配re    或者d,dd,ddd..
d

$ echo "re"|grep "\(re\)\|\(d\+\)"
re

$ echo "a"|grep "\(re\)\|\(d\+\)"       //没有匹配上
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值