How to Exclude in Grep
Pattern matching is a fundamental and powerful tool for searching and matching text data using features like regular expressions.
If you are unfamiliar with grep
, it is a powerful command-line utility in Linux and other Unix-based systems. It allows us to search plain text data for matching patterns using regex
.
However, when working with grep,
we may not want to find the matching lines; instead, we wish to exclude the lines that match a specific pattern. This is where the grep
exclude feature comes into aid.
In this tutorial, we will learn how to use the features provided in grep
and regex
to exclude the matching patterns from a given search query.
Using Grep
Let us start with the basics and understand how to use the grep
command. We are assuming you have the grep command installed on your system.
To search for a specific pattern using grep
, we call the grep command followed by the pattern we wish to search and, finally, the text file on which we want to locate the matching pattern.
An example syntax is as shown:
grep "pattern" filename.txt
Suppose we have a file that contains a list of databases, as shown below:
MySQL
PostgreSQL
SQL Server
SQLite
We can use the grep
command to locate all the entries with the matching string SQL
as shown in the command output below:
grep "SQL" databases.txt
Once we run the above command, it should print all the lines in the databases.txt
file with the matching pattern. An example output is as shown:
In this case, we can see that the matching patterns are colored in the output. Remember that this might not always be the case, especially where we do not have colored terminal output. However, in this case, the result is accessible to read and notice the pattern where it occurs.
Now that you know the basics of searching patterns using grep
, let us proceed and learn how to exclude specific patterns from the search result.
Grep Exclude Pattern - Using the -v
Option
In the grep
manual, we have access to the -v
option, which tells grep to invert the search. You can also use the --invert-search
option for a more readable format.
Using this option, we tell grep
that it should return the lines that do not match the given pattern.
For example, suppose we extend the entries in the database.txt
file to include the following options:
MySQL
PostgreSQL
SQL Server
SQLite
Redis
Cassandra
MongoDB
Grep also allows us to search for multiple patterns in a given query using the -e
option.
We now want to find all the entries in the databases.txt
file that do not match the word SQL
. We can use the -v
option but with the same pattern as shown:
╭─csalem@captains-MacBook-Pro ~/docker-basics
╰─$ grep -v "SQL" databases.txt
Redis
Cassandra
MongoDB
In this case, the query above returns the lines that do not contain the string ‘SQL’ as shown from the output above.
Specifying Multiple Patterns
Grep also allows us to specify multiple patterns using the -e
option for each pattern. This means we can create more granular and precise searches or exclusions by combining them with the -v
option.
For example, taking the entries in the databases.txt file, we can exclude all the lines containing the string SQL
and Redis,
as shown in the command below:
$ grep -v -e "SQL" -e "Redis" databases.txt
Cassandra
MongoDB
As you can guess, the command will exclude all the entries containing the string SQL or Redis from the output:
Using Extended Regular Expressions
Yes, there are more complex methods of excluding specific patterns. If you are familiar with regular expressions, specify them to create even more precise and intricate pattern matching using the -E
option.
For example, we can use the |
operator in regex
to exclude multiple patterns in a single entity, as demonstrated below:
$ grep -E -v "SQL|Redis" databases.txt
Cassandra
MongoDB
As you can see from the resulting output, the command above excludes all the entries containing the string SQL
and Redis
.
Specifying Exclusions in a File
In other cases, we can list all the patterns we wish to exclude defined in an external file. This is especially powerful if the file you want to search is large, such as system logs.
In such a case, you can simply point grep
to the path with the pattern you wish to exclude in the command, as shown:
grep -v -f exclude.txt database.txt
The exclude.txt file contains the patterns you wish to exclude in the resulting set.
Exclusion Before and After Match
As you can imagine, in the real world, you might encounter more complex scenarios that exclude the matching lines from the output.
For example, you might have an instance where you need to exclude the lines that comes before a specific match. This is where the -B
option comes into play.
Using the -B
option tells grep that we only want to exclude the specified number of lines before a match.
For example, to exclude the line containing the string SQL
and the two lines that come before it, we can use the -B
option as shown:
$ grep -v -B2 "SQL" databases.txt
SQL Server
SQLite
Redis
Cassandra
MongoDB
We can also use the -A
option to exclude after, as shown in the example below:
$ grep -v -A2 "SQL" databases.txt
Redis
Cassandra
MongoDB
Excluding a Specific Length
We can also take advantage of regular expressions to exclude lines of a specific length, as demonstrated in the example command below:
$ grep -E -v "^.{5}$" databases.txt
PostgreSQL
SQL Server
SQLite
Cassandra
MongoDB
This tells grep
to remove all the lines of length five and below.
Conclusion
In this tutorial, you learned some basic methods for using the -v
option with grep
and regular expressions to exclude specific patterns from the resulting output. While the methods discussed in this tutorial are crucial in understanding the workings of grep, grep is more powerful, especially when paired with complex regex queries.