含义
即 global regular expressions print,即找出并打印与所给的正则表达式相匹配的文件内容
语法
grep [options] [pattern] [files]
参数选项
参数选项 | 含义 | 功能 |
---|---|---|
-A n | after context | 即输出匹配行的后 n 行 |
-B n | before context | 即输出匹配行的前 n 行 |
-E | extended regexp | 允许正则匹配 |
-c | count | 统计匹配次数 |
-i | ignore case | 忽略大小写 |
-n | line number | 输出行号 |
-v | invert match | 反向查找 |
--color | color | 高亮显示关键字,有三种模式 [always/never/auto] |
Example
# print 4 lines after or before matching
cat text.txt | grep -A/B 4 'hello'
# print line number, count of matching while ignoring case
cat text.txt | grep -cni 'uzi'
...
掌握语法和选项,根据需要选择即可,可以进一步了解 LinuxShell 的其他管道命令