How to Exclude a Word with grep

The grep command line tool is wildly useful for searching through text data for lines and snippets that match a defined string, character, word, or regular expression. While most uses of grep are for sorting data for syntax matches, what if you want to exclude a word or string with grep instead? Excluding line matches with grep is equally as useful as finding and printing matches in grep, so let’s cover how to exclude string matches and exclude words with grep.

Obviously you’ll want to have some command line experience and exposure to grep to find this useful. If you want to follow along, you can open the Terminal application and try it out yourself. Since grep is an OS agnostic utility, you can use the exclude trick in Mac OS, Linux, unix, or whatever else you have that uses grep.

How to Exclude a Single Word with grep

The most simple way to exclude lines with a string or syntax match is by using grep and the -v flag.

For example, let’s say we’re using cat to print a file at the command line, but we want to exclude all lines that include the term “ThisWord”, then the syntax would look as follow:

cat example.txt | grep -v "ThisWord"

The output will be the example.txt text file but excluding any line that contains a string match with “ThisWord”.

You can also use grep directly on files and exclude line matches based on words or syntax, like so:

grep -v "ThisWord" example.txt

Use whichever works best for your particular workflow.

How to Exclude Multiple Strings or Words with grep

Now that you know how to exclude matches for a single word, the next obvious question is about excluding multiple words with grep. That’s equally as simple, and there are a few different ways to accomplish this using the -v flag as well as the -e flag.

First lets take the above example of using cat on a file piped to grep, and exclude any lines matching two words; “Word1” and “Word2”, this would look like the following:

cat example.txt | grep -v -e "Word1" -e "Word2"

Any lines that contain “Word1” or “Word2” will be excluded from the printed results.

You can also use grep directly on files just as before as well:

grep -v -e "Word1" -e "Word2" example.txt

Another approach is to separate what to exclude with grep by using a pipe to separate each match, like so:

grep -Ev "word1|word2" example.txt

If you test out any of these options on an example text file, you will find the output is identical regardless of the approach you take, each excluding lines that include the targeted phrases, syntax, words, or text match.

In the example here we’ll print command history for defaults string matches, but exclude anything matching having to do with iTunes as defined by “com.apple.itunes”:

history |grep "defaults write" |grep -v -e "com.apple.itunes"

So if you’ve been following along, this would report back all historical executions of the “defaults write” command, but excluding anything that pertained to the iTunes application. Nice huh?

If you have any particularly handy uses of excluding matches with grep, share them with us in the comments below! And if you enjoyed this article, you’d almost certainly like browsing through our many command line articles here where there is plenty more to learn!

This website and third-party tools use cookies for functional, analytical, and advertising purposes. By continuing to browse the site, closing this banner, scrolling this webpage, or clicking a link, you agree to these cookies. You can review our privacy policy for additional information.I AcceptPrivacy Policy