Exclude Words Or Strings Via Grep Command

0
30
Exclude Words Or Strings Via Grep Command

Do you want to exclude words or strings via grep? The grep command-line tool is very essential for searching through text data for snippets or lines that match a character, defined string, word, or regular expression. However, some use grep for sorting data for syntax matches, what if you like to exclude a string or word with grep instead? Removing line matches with grep is as essential as searching and printing matches in grep. So come let’s take a look at how to exclude string matches and exclude words with grep.

However, you need some command-line experience to grep to find this essential. Head over to the Terminal application and try it out yourself. However, grep is an OS-agnostic utility, you can simply use the exclude trick in Mac OS, Unix, Linux, or other else you have that uses grep.

Exclude a Single Word via grep

The easiest way to exclude lines with syntax or string match is after using grep and the -v flag.

For instance, We’re using cat to print a file at the command-line, but we wish to exclude all lines that add the term “ThisWord”, then the syntax is given follow:

cat example.txt | grep -v "ThisWord"

The output you receive is example.txt text file but after excluding any line having a string match with “ThisWord”.

Also, you can use grep directly on files then exclude line matches based on syntax or words, like so:

grep -v "ThisWord" example.txt

Exclude Several Strings or Words via grep

You must know how to exclude matches for a single word, the other question is about excluding various words with grep. That’s quite simple. Also, there are various methods to fulfill this using the -v flag as well as the -e flag.

Initially, move further with the above instance of using a cat on a file piped to grep. Also, exclude lines matching two words; “Word1” and “Word2”, this would seem like the following:

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

Any lines having “Word1” or “Word2” can be excluded from the printed results.

Also, you can use grep directly on files before as well:

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

Another method is to separate or divide what to exclude via grep after using a pipe to separate each match, like so:

grep -Ev "word1|word2" example.txt

After testing any of these options on an instance text file. Then you must find the output is quite similar besides the approach you take. Every excluding line that adds the targeted phrases, words, syntax, or text match.

Great, show me an essential example of excluding data via grep!

For a useful example that Mac users may find quite good. We can use grep exclusion while querying or printing command line history to find earlier run commands to find default matches. But after excluding some chosen defaults strings from the output.

In the example mentioned below, we’ll print command history for default string matches. But you can exclude anything matching to do with iTunes as mentioned by “com.apple.iTunes”:

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

It reports back all historical executions of the “defaults write” command, but eliminate anything that concerns to the iTunes application. Nice huh?

Conclusion:

Here’s all about “Steam Not Opening”. What are your views about it? Is this article helpful or not? Let us know your thoughts and suggestions in the comment section below!

Also Read:

LEAVE A REPLY

Please enter your comment!
Please enter your name here