linux grep command – grep not include, grep reverse match

There is also a case where we need to exclude certain content when we do content search.

How to achieve it?

Still linux grep command!

I have such a file test5.txt.

Need to query the line containing “dfff” content but not include “apple”.

First, use linux grep to query the line containing “dfff”.

➜  grep -n "dfff" test5.txt

Find 3 lines with “dfff”, we just need to exclude the content “apple”.

How to do it?

Use linux pipe command and linux grep -v

➜  grep -n "dfff" test5.txt | grep -v "apple"

The above statement, the meaning is to first match the line containing “dfff”, then put the structure inside the pipeline, and finally exclude the line containing “apple”

Of course, you can also exclude “apple” and then match “dfff”

➜  grep -n -v "apple" test5.txt| grep "dfff"

More about grep:Linux grep command

More about Linux commands: Linux Commands Tutorial

Add a Comment

Your email address will not be published.