grep [选项] ”模式“ [文件]
常用选项
| 选项 | 含义 |
|---|---|
| -E | 开启扩展(Extend)的正则表达式。 |
| -i | 忽略大小写(ignore case)。 |
| -v | 反过来(invert),只打印没有匹配的,而匹配的反而不打印。 |
| -n | 显示行号 |
| -w | 被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker |
| -c | 显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项是显示有多少行没有被匹配到。 |
| -o | 只显示被模式匹配到的字符串。 |
| --color | 将匹配到的内容以颜色高亮显示。 |
| -A n | 显示匹配到的字符串所在的行及其后n行,after |
| -B n | 显示匹配到的字符串所在的行及其前n行,before |
| -C n | 显示匹配到的字符串所在的行及其前后各n行,context |
[yangyang@izuf6btm1dq2w64mt5q889z practice]$ cat log.txt
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo
输出含有“this”的行(-n会输出行号)
[yangyang@izuf6btm1dq2w64mt5q889z practice]$ grep this log.txt
2 this is a test
[yangyang@izuf6btm1dq2w64mt5q889z practice]$ grep -n this log.txt
1:2 this is a test
输出含有“a”单词的行
[yangyang@izuf6btm1dq2w64mt5q889z practice]$ grep -wn is log.txt
1:2 this is a test