Shell编程之正则表达式-grep使用

一、Shell编程之正则表达式-grep用法

1、从文件中查找出特定字符“the”所在位置

其中“-n”表示显示行号、“-i”表示不区分大小写。

grep -n ‘the’ test.txt

mark

2、从文件中查找出不区分大小写的特定字符“the”所在位置

grep -in ‘the’ text.txt

mark

3、反向选择,查找不包含“the”字符的行

grep -vn ‘the’ text.txt

mark

4、想要查找“shirt”与“short”这两个字符串的行

grep -n ‘sh[io]rt’ test.txt

mark

5、若要查找包含重复单个字符“oo”的行

grep -n ‘oo’ test.txt

mark

6、查找"oo"前面不是“w”的字符串的行

grep -n ‘[^w]oo’ test.txt

mark

7、不希望“oo”前面存在小写字母的行

grep -n ‘[^a-z]oo’ test.txt

mark

8、查找包含数字的行

grep -n ‘[0-9]’ test.txt

mark

9、查找以“the”字符串为行首的行

grep -n ‘^the’ test.txt

mark

10、查询以小写字母开头的行

grep -n ‘^[a-z]’ test.txt

mark

11、查询以大写字母开头的行

grep -n ‘^[A-Z]’ test.txt

mark

12、不以字母开头的行

grep -n ‘^[^a-zA-Z]’ test.txt

mark

13、实现查询以小数点结尾的行

grep -n ‘\.$’ test.txt

mark

14、查询空白行

grep -n ‘^$’ test.txt

mark

15、查询以w开头d结尾的字符串的行

grep -n ‘w…d’ test.txt

mark

16、查询包含至少两个o以上的字符串的行

grep -n ‘ooo*’ test.txt

mark

17、查询以w开头d结尾,中间包含至少一个o的字符串的行

grep -n ‘woo*d’ test.txt

mark

18、查询以w开头d结尾,中间的字符可有可无的字符串的行

grep -n ‘w.*d’ test.txt

mark

19、查询任意数字所在行

grep -n ‘[0-9][0-9]*’ test.txt

mark

20、查找两个o的字符串的行

grep -n ‘o\{2\}’ test.txt

mark

21、查询以w开头以d结尾,中间包含2~5个o的字符串的行

grep -n ‘wo\{2,5\}d’ test.txt

mark

22、查询以w开头以d结尾,中间包含2个或2个以上o的字符串的行

grep -n ‘wo\{2,\}d’ test.txt

mark

二、使用总结

1、基础正则表达式常见元字符

mark

2、扩展正则表达式常见元字符

mark

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值