linux grep or , grep and , grep not condition examples

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

➜  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

grep and condition

Use pipe

➜  grep -n -C 3  "A num" man-grep.txt | grep  "B num"

grep not condition

Use grep -v

➜ grep -n "A num" man-grep.txt | grep -v "B num"

Add a Comment

Your email address will not be published.