zhoufcumt的个人博客分享 http://blog.sciencenet.cn/u/zhoufcumt

博文

grep用法 扩展 from 《鸟哥的Linux私房菜》

已有 4306 次阅读 2013-3-31 23:27 |个人分类:LINUX|系统分类:科研笔记| 扩展, grep

grep真是一个很棒的工具,不解释,体验下。


实例文件:

regular_express.txt


例题1:查找特定字符串

查找包括“the”的

[zhouforme@zhouforme ~]$ grep -n 'the' regular_express.txt

8:I can't finish the test.

12:the symbol '*' is represented as start.

15:You are the best is mean you are the no. 1.

16:The world <Happy> is the same with "glad".

18:google is the best tools for search keyword.


不区分大小写

[zhouforme@zhouforme ~]$ grep -in 'the' regular_express.txt

8:I can't finish the test.

9:Oh! The soup taste good.

12:the symbol '*' is represented as start.

14:The gd software is a library for drafting programs.

15:You are the best is mean you are the no. 1.

16:The world <Happy> is the same with "glad".

18:google is the best tools for search keyword.


查找不包括“the”的(反向查找)

[zhouforme@zhouforme ~]$ grep -vn 'the' regular_express.txt


例题2:利用中括号[]来查找集合字符

比如要查找test或taste,它们有共同的“t?st”

[zhouforme@zhouforme ~]$ grep -n 't[ae]st' regular_express.txt

8:I can't finish the test.

9:Oh! The soup taste good.


如果想要查找有oo字符时,

[zhouforme@zhouforme ~]$ grep -n 'oo' regular_express.txt

1:"Open Source" is a good mechanism to develop programs.

2:apple is my favorite food.

3:Football game is not use feet only.

9:Oh! The soup taste good.

18:google is the best tools for search keyword.

19:goooooogle yes!


如果不想要oo前面有g,

[zhouforme@zhouforme ~]$ grep -n '[^g]oo' regular_express.txt

2:apple is my favorite food.

3:Football game is not use feet only.

18:google is the best tools for search keyword.

19:goooooogle yes!


如果不想要oo前面有小写字符,

[zhouforme@zhouforme ~]$ grep -n '[^a-z]oo' regular_express.txt

3:Football game is not use feet only.


或者

[zhouforme@zhouforme ~]$ grep -n '[^[:lower:]]oo' regular_express.txt

3:Football game is not use feet only.


对于数字,

[zhouforme@zhouforme ~]$ grep -n '[0-9]' regular_express.txt

5:However, this dress is about $ 3183 dollars.

15:You are the best is mean you are the no. 1.


或者

[zhouforme@zhouforme ~]$ grep -n '[[:digit:]]' regular_express.txt

5:However, this dress is about $ 3183 dollars.

15:You are the best is mean you are the no. 1.


例题3:行首与行尾字符^$

如果只显示在行首出现the的,

[zhouforme@zhouforme ~]$ grep -n '^the' regular_express.txt

12:the symbol '*' is represented as start.


如果要开头是小写字符的,

[zhouforme@zhouforme ~]$ grep -n '^[a-z]' regular_express.txt

2:apple is my favorite food.

4:this dress doesn't fit me.

10:motorcycle is cheap than car.

12:the symbol '*' is represented as start.

18:google is the best tools for search keyword.

19:goooooogle yes!

20:go! go! Let's go.


或者

[zhouforme@zhouforme ~]$ grep -n '^[[:lower:]]' regular_express.txt

2:apple is my favorite food.

4:this dress doesn't fit me.

10:motorcycle is cheap than car.

12:the symbol '*' is represented as start.

18:google is the best tools for search keyword.

19:goooooogle yes!

20:go! go! Let's go.


NOTE: 注意与上面的区别!^在[]内代表“反向选择”,在[]外则代表定位在行首的意义!


如果不想开头是英文字符,

[zhouforme@zhouforme ~]$ grep -n '^[^a-zA-Z]' regular_express.txt

1:"Open Source" is a good mechanism to develop programs.

21:# I am Vbird.


或者

[zhouforme@zhouforme ~]$ grep -n '^[^[:alpha:]]' regular_express.txt

1:"Open Source" is a good mechanism to develop programs.

21:# I am Vbird.







https://blog.sciencenet.cn/blog-350278-675725.html

上一篇:win7下ultraedit+fortran高亮
下一篇:科学网博客不再更新!
收藏 IP: 222.66.117.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-9-27 19:27

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部