linux 中grep使用方法,linux中grep使用方法具体解释

本文介绍了在Linux环境下使用grep命令结合正则表达式进行文本搜索的多种方法,包括查找特定字符串并高亮显示、反向选择、忽略大小写、查找集合字符、过滤特定前缀、查找非小写字母开头的字符串、提取含数字的行、匹配特定模式等。通过实例展示了正则表达式的强大功能和灵活性。
摘要由CSDN通过智能技术生成

查找特定字符串并颜色显示

[root@fwq test]# grep -n 'the' regular_express.txt --color=auto8: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 is the same with "glad".

18:google is the best tools for search keyword.

反向选择查找特定字符串并颜色显示

[root@fwq test]# grep -vn 'the' regular_express.txt --color=auto1:"Open Source" is a good mechanism to develop programs.

2:apple is my favorite food.

......

忽略大写和小写查找特定的字符

[root@fwq test]# grep -in 'the' regular_express.txt

使用 [] 来查找集合字符:

[root@fwq test]# grep -n 't[ae]st' regular_express.txt8:I can't finish the test.

9:Oh! The soup taste good.

查找有‘oo’字符串

[root@fwq test]# grep -n 'oo' regular_express.txt1:"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的,即剔除goo

[root@fwq test]# grep -n '[^g]oo' regular_express.txt2: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的内容

[root@fwq test]# grep -n '[^a-z]oo' regular_express.txt3:Football game is not use feet only.

获取有数字的一行

[root@fwq test]# grep -n '[0-9]' regular_express.txt5:However, this dress is about $ 3183 dollars.

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

[root@fwq test]# grep -n '[^[:lower:]]oo' regular_express.txt3:Football game is not use feet only.

查询以the开头的字符

[root@fwq test]# grep -n '^the' regular_express.txt12:the symbol '*' is represented as start.

查询开头是小写字母的内容

[root@fwq test]# grep -n '^[a-z]' regular_express.txt2: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.

查询第一个字符不是大写的

[root@fwq test]# grep -n '^[[:lower:]]' regular_express.txt2: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.

查询不是以英文开头的字符

[root@fwq test]# grep -n '^[^a-zA-Z]' regular_express.txt

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

21:# I am VBird

查询结尾是小数点的行的内容.

[root@fwq test]# grep -n '\.$' regular_express.txt1:"Open Source" is a good mechanism to develop programs.

2:apple is my favorite food.

3:Football game is not use feet only.

4:this dress doesn't fit me.

10:motorcycle is cheap than car.

11:This window is clear.

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

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

16:The world is the same with "glad".

17:I like dog.

18:google is the best tools for search keyword.

20:go! go! Let's go.

查找“空白行”

[root@fwq test]# grep -n '^$' regular_express.txt22:

通配符.和*的使用,.(小数点)代表一定有一个随意字符,*代表反复前一个到无穷多次的意思

[root@fwq test]# grep -n 'g..d' regular_express.txt

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

9:Oh! The soup taste good.

16:The world is the same with "glad".

查询出现随意数字的行列

[root@fwq test]# grep -n '[0-9][0-9]*' regular_express.txt5:However, this dress is about $ 3183 dollars.

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

查询出现两个o的字符串

[root@fwq test]# grep -n 'o\{2\}' regular_express.txt1:"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!

查询出现2-5个o的字符串,后面在接一个g的字符串

[root@fwq test]# grep -n 'o\{2,5\}g' regular_express.txt

18:google is the best tools for search keyword.

19:goooooogle yes!

查询出现2个以上o的字符串,后面在接一个g的字符串

[root@fwq test]# grep -n 'o\{2,\}g' regular_express.txt

18:google is the best tools for search keyword.

19:goooooogle yes!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值