linux正则表达式grep,Linux下grep与正则表达式

本文介绍了如何在Linux环境中使用grep命令配合正则表达式进行文本过滤和查找。通过实例展示了如何查找特定字符串、排除特定字符、忽略大小写、查找集合字符以及利用行首和行尾符号进行匹配。同时,提到了Linux与Windows文件的断行字符差异,提醒读者在跨平台操作时需要注意文件格式问题。
摘要由CSDN通过智能技术生成

在Linux下,我们经常会使用grep对一些结果进行过过滤,下面简单介绍一个grep和正则表达式结合的用法。

源文件如下:

1 "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.$

5 However, this dress is about $ 3183 dollars.^M$

6 GNU is free air not free beer.^M$

7 Her hair is very beauty.^M$

8 I can't finish the test.^M$

9 Oh! The soup taste good.^M$

10 motorcycle is cheap than car.$

11 This window is clear.$

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

13 Oh!^IMy god!$

14 The gd software is a library for drafting programs.^M$

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.$

19 goooooogle yes!$

20 go! go! Let's go.$

21 # I am VBird$

22 $

查找特定字符串

查找the这个特定字符串:grep -n 'the' regular_express.txt

786f421026ff

grep -n 'the' regular_express.txt

查找不包含the这个字符的行:grep -vn 'the' regular_express.txt

786f421026ff

grep -vn 'the' regular_express.txt

忽略大小写查找包含the的行:grep -in 'the' regular_express.txt

786f421026ff

grep -in 'the' regular_express.txt

利用中括号[]来查找集合字符

比如我想查找test或者taste这两个单词时,可以这么写:grep -n 't[ae]st' regular_express.txt (不论方括号里面有几个字符,它都只代表一个字符

786f421026ff

grep -n 't[ae]st' regular_express.txt

如果不想要oo前面有g的话,可以这么写:grep -n '[^g]oo' regular_express.txt ([^g]中的^表示反向选择,即表示oo前面不能是g)

786f421026ff

grep -n '[^g]oo' regular_express.txt

假如我oo前面不能有小写字符:grep -n '[^a-z]oo' regular_express.txt (a-z就表示小写字符,还有另外一种写法使用特殊字符[:lower:],这个也是表示小写字母-grep -n '[^[:lower:]]oo' regular_express.txt)

786f421026ff

grep -n '[^a-z]oo' regular_express.txt

行首和行尾字符^ 和 $

查找到一行中行首为the的:grep -n '^the' regular_express.txt

786f421026ff

grep -n '^the' regular_express.txt

查找不以小写字符开头的:grep -n '[a-z]' regular_express.txt(这里需要注意^ 在[]的里面和外面是两个不同的概念,在[]里面表示反向选择,在[]外面表示定位在行首的意义)

786f421026ff

grep -n '^[^a-z]' regular_express.txt

查找行尾是. d的: grep -n '.$' regular_express.txt, 这里有两个点需要注意第一是小数点.之前有一个转移符,这是因为.具有其他意义,因此需要使用转移符\来消除其特殊意义。第二点就是文件中的第5-9行并没有被打印出来。我们如果仔细看源文件的话或发现第5-9行的结尾是.^M$,这里涉及到一个Linux和windows的断行字符的问题,Linux是以LF(\n)断行,而windows是以CRLF(\r\n)断行,所以一般我们在Linux上面查看windows的文件,其结尾都是^M,所以我们在windows下编写的脚本放到Linux上运行之前需要小心,要去除掉这个字符,不然运行会报错。

786f421026ff

grep -n '.$' regular_express.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值