Usually we need to use a condition query when looking up keywords.
Through this article you will learn how to use the linux grep command to perform or/and/not condition queries for keywords.
grep or condition
1. Use regular
➜ grep -n -C 3 "[A|B] num" man-grep.txt
![](http://www.linuxcommands.site/wp-content/uploads/2019/05/image-12-1024x626.png)
➜ grep -n -C 3 -E "[A|B] num" man-grep.txt The results are consistent.
2. Use grep -e
With the grep -e option, only one parameter can be passed. Use multiple -e options in a single command to get multiple patterns to implement OR operations.
➜ grep -n -e "A num" -e "B num" -C 3 man-grep.txt
![](http://www.linuxcommands.site/wp-content/uploads/2019/05/image-14-1024x664.png)
grep and condition
Use pipe
➜ grep -n -C 3 "A num" man-grep.txt | grep "B num"
![](http://www.linuxcommands.site/wp-content/uploads/2019/05/image-13-1024x73.png)
grep not condition
Use grep -v
➜ grep -n "A num" man-grep.txt | grep -v "B num"
![](https://i0.wp.com/www.linuxcommands.site/wp-content/uploads/2019/06/image-40.png?fit=900%2C142&ssl=1)