linux-grep

--version or -V grep的版本

-A 数字N找到所有的匹配行,并显示匹配行后N

-B 数字N找到所有的匹配行,并显示匹配行前面N

-b显示匹配到的字符在文件中的偏移地址

-c显示有多少行被匹配到

--color把匹配到的字符用颜色显示出来

-e可以使用多个正则表达式

-f FILEA FILEB FILEAFILEAB中的匹配

-i不区分大小写针对单个字符

-m 数字N最多匹配N个后停止

-n打印行号

-o只打印出匹配到的字符

-R搜索子目录

-v显示不包括查找字符的所有行




[root@localhost test]# cat test.txt 

a

b

c

d

123

456

789

aaa

bbb

ccc

ddd

eee

(*%#@^%$)

aa

bb

cc

dd

1234567890

qwertyuiop

asdfghjkl

zxcvbnm


1.-A

找到所有的匹配行,并显示匹配行后面N

格式:grep -A number document

[root@localhost test]# grep -A  1 a test.txt 

a

b

--

aaa

bbb

--

aa

bb

--

asdfghjkl

zxcvbnm


2.-B 

格式:grep -B number document

找到所有匹配的行,并显示匹配行前面N

[root@localhost test]# grep -B 1 c test.txt 

b

c

--

bbb

ccc

--

bb

cc

--

asdfghjkl

zxcvbnm


3.-b

格式:grep -b text document

显示匹配到的字符在文件中的偏移地址

[root@localhost test]# grep -b a test.txt 

0:a

20:aaa

50:aa

84:asdfghjkl


4.-c

格式:grep -c text document

显示有多少行被匹配到

[root@localhost test]# grep -c a test.txt 

4


5.--color

把匹配到的字符用颜色显示出来

[root@localhost test]# grep --color b test.txt 

Screen Shot 2017-10-11 at 2.40.55 PM.png


6.-e

可以使用多个正则表达式

[root@localhost test]# grep -e a -e b test.txt 

a

b

aaa

bbb

aa

bb

asdfghjkl

zxcvbnm


7.-f

FILE_AFILEA_B中的匹配

[root@localhost test]# vi t1

a

b

AA

BB

1234567

[root@localhost test]# vi t2

b

BB

abc

[root@localhost test]# grep -f t1 t2 

b

BB

abc


8.-i

不区分大小写

[root@localhost test]# grep -i a t1

a

AA


9.-m

最多匹配N个后停止,实际上一般是匹配到N个后的那一行停止

[root@localhost test]# grep -m 2 a test.txt 

a

aaa


10.-n

显示行号

[root@localhost test]# grep -m 2 a -n test.txt 

1:a

8:aaa


11.-o

只会打印匹配到的字符

[root@localhost test]# grep -o a test.txt 

a

a

a

a

a

a

a



12.-R


只会在当前目录查找

[root@localhost test]# grep "a" *

a.sh:aa bb

cc.log:aaa

cc.log:a

t1:a

t2:abc

test.txt:a

test.txt:aaa

test.txt:aa

test.txt:asdfghjkl



在当前目录以及子目录查找

[root@localhost test]# grep -R aa *

a.sh:aa bb

cc.log:aaa

test.txt:aaa

test.txt:aa


显示不包括查找字符的所有行

[root@localhost test]# grep -v AA t1 

a

b

BB

1234567



—————————————

pattern主要参数

参数解释

^ 匹配行首

$ 匹配行尾

[ ] or [ n - n ] 匹配[ ]内字符

.匹配任意的单字符

*紧跟一个单字符,表示匹配0个或者多个此字符

\用来屏蔽元字符的特殊含义

\?匹配前面的字符0次或者1

\+匹配前面的字符1次或者多次

X\{m\} 匹配字符X m

X\{m,\} 匹配字符X最少m

X\{m,n\} 匹配字符X m---n

666标记匹配字符,如666被标记为1,随后想使用666,直接以 1代替即可

\|表示或的关系


【案例】


[root@localhost test]# cat test.txt 

a

abc

ABCD

1

1234

abcd1234

12789Aabcdfepomz


1.^ 匹配行首

[root@localhost test]# grep -n '^a' test.txt 

1:a

2:abc

6:abcd1234


2.$ 匹配行尾

[root@localhost test]# grep -n 'CD$' test.txt 

3:ABCD


3. [ ]

匹配 [ ]内的字符

注意下面的执行是错误的

[root@localhost test]# grep -n [ a-b 7-9 ] test.txt 

grep: Invalid regular expression

[root@localhost test]# grep -n '[ a-b 7-9 ]' test.txt 

1:a

2:abc

6:abcd1234

7:12789Aabcdfepomz



4.  .

匹配任意的字符

[root@localhost test]# grep -n '.a' test.txt 

7:12789Aabcdfepomz


5. *

紧跟一个单字符,表示匹配0个或者多个此字符


Screen Shot 2017-10-11 at 6.05.18 PM.png


6. \  

用来屏蔽元字符的特殊含义

可以在文本内增加自己喜欢的内容,进行测试

[root@localhost test]# grep -n '\$' test.txt 

9:$


7. \?

匹配前面的字符0此或者多次

Screen Shot 2017-10-11 at 6.09.59 PM.png


8.  \+

匹配前面的字符1此或者多次

[root@localhost test]# grep -n --color '3\+' test.txt 

5:1234

6:abcd1234


9.  X\{m\}

匹配字符X m 次

Screen Shot 2017-10-11 at 6.40.44 PM.png


10. X\{m,\}

匹配字符X 最少m次

[root@localhost test]# grep -n --color 'ab\{1,\}' test.txt 

同上


11.X\{m,n\

匹配字符X m-n

Screen Shot 2017-10-11 at 6.42.48 PM.png


12.\|

表示或的关系

Screen Shot 2017-10-11 at 6.44.41 PM.png


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值