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

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

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

grep -n ‘the’ test.txt

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

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

grep -in ‘the’ text.txt

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

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

grep -vn ‘the’ text.txt

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

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

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

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

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

grep -n ‘oo’ test.txt

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

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

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

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

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

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

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

8、查找包含数字的行

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

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

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

grep -n ‘^the’ test.txt

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

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

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

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

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

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

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

12、不以字母开头的行

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

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

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

grep -n ‘\.$’ test.txt

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

14、查询空白行

grep -n ‘^$’ test.txt

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

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

grep -n ‘w…d’ test.txt

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

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

grep -n ‘ooo*’ test.txt

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

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

grep -n ‘woo*d’ test.txt

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

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

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

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

19、查询任意数字所在行

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

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

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

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

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

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

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

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

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

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

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

二、使用总结

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

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

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

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

相关文章:

  • 2021-05-24
  • 2021-12-12
  • 2022-12-23
  • 2021-12-13
  • 2021-11-27
猜你喜欢
  • 2022-12-23
  • 2021-08-25
  • 2021-12-13
  • 2022-12-23
  • 2021-11-10
  • 2021-10-25
  • 2022-12-23
相关资源
相似解决方案