示例
假设我们有一个sample.txt 文件,内容如下:
jack@onitroad:/tmp$ cat sample.txt
Orange
white
blue
Blue line
black
red
Red Apple
green leaves
yellow
violet
pink rose
jack@onitroad:/tmp$
查找black或者red
jack@onitroad:/tmp$ grep 'black|red' sample.txt
black
red
jack@onitroad:/tmp$
精确查找
使用-w选项精确匹配关键字,精确匹配是区分大小写的。
jack@onitroad:/tmp$ grep -w 'black|red|blue' sample.txt
blue
black
red
jack@onitroad:/tmp$
查找时不区分大小写。
使用-i选项,在查找时不区分大小写。
jack@onitroad:/tmp$ grep -i 'blue|red|black' sample.txt
## 使用egrep替代
jack@onitroad:/tmp$ egrep -i 'blue|red|black' sample.txt
blue
Blue line
black
red
Red Apple