How to Exclude a Word with grep

Apr 5, 2018 - 3 Comments

Terminal in macOS

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.

Great, show me a useful example of excluding data with grep!

For a practical example that advanced Mac users may find helpful, we can use grep exclusion when printing and querying command line history to find previously executed commands to find defaults matches, but excluding some selected defaults strings from the output.

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!

.

Related articles:

Posted by: Paul Horowitz in Command Line, Tips & Tricks

3 Comments

» Comments RSS Feed

  1. Linux Guy says:

    Take a look at the grep commandline options “–exclude” and “–include”. Example: you want to find all instances of “ODataRequestContext” in the .java files in SDL’s example app, but not be bothered with HTML files, or worse, binary .class files.

    linuxguy@linuxguy:~/SDL/odata-example$ grep -nr ODataRequestContext –include “*java” *

    example-datasource/src/main/java/com/sdl/odata/example/datasource/StrategyBuilder.java:39:import com.sdl.odata.api.service.ODataRequestContext;
    example-datasource/src/main/java/com/sdl/odata/example/datasource/StrategyBuilder.java:64: public List<Predicate> buildCriteria(QueryOperation queryOperation, ODataRequestContext requestContext)
    example-datasource/src/main/java/com/sdl/odata/example/datasource/InMemoryDataSourceProvider.java:27:import com.sdl.odata.api.service.ODataRequestContext;

    This has the advantage that piping thru ‘grep -v’ clobbers color highlighting, and may be rejecting lines based on strings you did not expect it to.

  2. Rose says:

    Been looking for this solution for some time… Thanks!

  3. Andy says:

    Very helpfull article. Thank you!

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site