Leif160519的blog Leif160519的blog

——————

目录
grep用法
/    

grep用法

如果你想在当前目录下 查找"hello,world!"字符串,可以这样:

grep -rn "hello,world!" *
  • * : 表示当前目录所有文件,也可以是某个文件名
  • -r 是递归查找
  • -n 是显示行号
  • -R 查找所有文件包含子目录
  • -i 忽略大小写

下面是一些有意思的命令行参数:

grep -i pattern files :不区分大小写地搜索。默认情况区分大小写, 
grep -l pattern files :只列出匹配的文件名, 
grep -L pattern files :列出不匹配的文件名, 
grep -w pattern files :只匹配整个单词,而不是字符串的一部分(如匹配‘magic’,而不是‘magical’), 
grep -C number pattern files :匹配的上下文分别显示[number]行, 
grep pattern1 | pattern2 files :显示匹配 pattern1 或 pattern2 的行, 
grep pattern1 files | grep pattern2 :显示既匹配 pattern1 又匹配 pattern2 的行。 

这里还有些用于搜索的特殊符号:

\<\>分别标注单词的开始与结尾。

例如:

grep man * 会匹配‘Batman’‘manic’‘man’等,

grep '\<man' *匹配‘manic’‘man’,但不是‘Batman’

grep '\<man\>' 只匹配‘man’,而不是‘Batman’‘manic’等其他的字符串。

'^':指匹配的字符串在行首,

'$':指匹配的字符串在行尾,


“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题grep用法
作  者Leif160519
出  处https://github.icu/articles/2019/09/06/1567761378867.html
关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!