Exclude Multiple Patterns With Grep on Linux


Introduction

Grep is a powerful command line utility on Linux that allows users to search for patterns in text files. It is widely used for tasks such as searching log files for specific strings or patterns, searching for specific lines in a configuration file, or extracting information from a large dataset. One of the useful features of grep is the ability to exclude multiple patterns from the search. This can be useful when you want to filter out irrelevant or unwanted results from your search. In this article, we will discuss how to exclude multiple patterns with grep on Linux. We will cover the different options and the syntax for excluding patterns and provide examples to illustrate how these options can be used in practical situations.

Excluding a Single Pattern with Grep

The easiest way to exclude a pattern from a grep search is to use the "-v" option. This option tells grep to reverse the search, which means it will display all lines that do NOT match the specified pattern. For example, suppose you have a text file called "file.txt" which contains the following lines −

Apple
Banana
cherry
grape
Mango
Pear

If we want to exclude the word "banana" from the search results, we can use the following command −

$ grep -v file "banana".txt

The output of this command will be the following −

Apple
cherry
grape
Linux
Mango
Pear

As you can see, the line containing the word "banana" has been excluded from the search results.

Exclude Multiple Patterns With Grep

To exclude multiple patterns from a grep search, we can use the "-e" option followed by the patterns we want to exclude. The "-e" option allows us to specify multiple templates, separated by a space.

For example, suppose you want to exclude the words "apple" and "cherry" from your search results. We can use the following command −

$ grep -v -e "apple" -e "cherry" file.txt

The output of this command will be −

Banana
grape
Mango
Pear

As you can see, lines containing the words "apple" and "cherry" were excluded from the search results.

Exclude Templates From a List

Another way to exclude multiple patterns from a grep search is to create a list of patterns in a separate file and then use the "-f" option to tell grep to read patterns from this file. For example, suppose we have a file called "exclude.txt" which contains a list of patterns we want to exclude from search results −

Apple
cherry
grape

We can use the following command to exclude these patterns from the search results −

$ grep -v -f exclude.txt file.txt

The output of this command will be as follows −

Banana
Mango
Pear

Excluding Patterns With Regular Expressions

Grep also allows us to use regular expressions to exclude patterns from search results. Regular expressions are a powerful tool that allow us to specify complex patterns using special characters and symbols. For example, suppose you want to exclude all lines that contain the word "grape" or any variant of it.

Sometimes we may only want to exclude a pattern if it appears as a separate word and not part of a larger word. In these cases, we can use the "\b" symbol to specify a word limit. For example, suppose you want to exclude the word "grapes" from your search results, but you want to include the word "grapefruit". We can use the following command to achieve this −

$ grep -v "\bgrape\b" file.txt

The output of this command will be −

Apple
Banana
cherry
pink grapefruit
Mango
Pear

As you can see, the line containing the word "grape" was excluded from the search results, but the line containing the word "grapefruit" was included.

Excluding Case-Insensitive Models

By default, grep is case-sensitive, which means that it treats "apple" and "apple" as two different models. However, we can use the "-i" option to make it case insensitive, so that "apple" and "Apple" are treated as the same pattern. For example, suppose you want to exclude the word "apple" from your search results, but you want to include lines that contain the word "apple". We can use the following command to achieve this −

$ grep -vi apple file.txt

The output will be −

Apple
Banana
cherry
grape
Mango
Pear

As you can see, the line containing the word "Apple" was included in the search results, even though it doesn't exactly match the "apple" pattern.

Conclusion

In this article, we discuss how to grep multiple models on Linux. We cover the different options and the syntax for excluding patterns, and provide examples to illustrate how these options can be used in practical situations. We also discuss how to use regular expressions, word limits, and case insensitivity to refine your search results. With these techniques, you should be able to effectively filter unwanted results from your grep searches.

Updated on: 22-Oct-2023

19K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements