linux grep命令使用详解

来源:互联网 发布:淘宝加入购物车看不到 编辑:程序博客网 时间:2024/04/25 20:30

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

grep的option

$cat a.txtabbcccdddd1a2b3d4a5a

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

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

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

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

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

$ grep -C 1 b a.txtabb             //匹配到b了,把a,ccc前后一行也显示出来ccc----1a2b3d

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

$ grep "HOST" a.pcapBinary file a.pcap matches$ grep -a "HOST" a.pcapHOST: 239.255.255.250:1900HOST: 239.255.255.250:1900

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

$grep -b  a a.txt0:a14:1a        //表示这一行的a在文件中的offset为1423:4a26:5a

-c 显示有多少行match

$grep -c a a.tx4                 

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

#grep --color a a.txta1a4a5a

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

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

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

$ cat pattern.txta2$ cat a.txtabbcccdddd1a2b3d4a5a2$grep -f pattern.txt  a.txta1a2b4a5a2

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

$ grep -m 2 a a.txta1a

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

$ grep -n a a.txt1:a5:1a8:4a9:5a

-o 只打印匹配的内容

$ grep -o a a.txtaaaa

-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 matchesa.txt:aa.txt:1aa.txt:4aa.txt:5ab.txt:aaagrep: dir1: Is a directory      //碰到一个目录了pattern.txt:a
$grep -R a *           //搜索子目录Binary file a.pcap matchesa.txt:aa.txt:1aa.txt:4aa.txt:5ab.txt:aaadir1/b.txt:a                  //这个目录也找到了pattern.txt:a

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

$ grep -v a a.txtbbcccdddd2b3d2

-w 查找一个词,

#cat word.txtit is a good day   //分成五个词,分别是it , is, a, good, dayitis a goodday     //分成三个词,分别是itis, a,  goodday$ grep -w it word.txt   it is a good day$ grep -w good word.txtit is a good day

grep 正则表达式

^pattern 表示开头处开始匹配

$echo "it is "|grep "^is"      //因为is不在行的开始处,所以没有匹配上$echo "it is "|grep "^i"it ispattern$ 从结尾处匹配         $ 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,aaaaa$ 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\+\)"       //没有匹配上