Linux Commands

How to Exclude Matches with grep

Grep stands for ā€œglobal regular expression printā€ has been a widely used command in the Linux platform. It has been utilized to do a lot of functions while using different flag keywords. The grep command with ā€œ-vā€ can be used in several ways to exclude the matches from the files. Letā€™s have a sharp look at each one of them one by one. Firstly you must have some text file to perform the grep command on it. So we have been using the ā€œnew.txtā€ file that exists in our home directory. Thus, we have to use the ā€œcatā€ query in the console to display the data of this file. The file contains a total of 6 lines, as illustrated in the image.

$ cat filename

Exclude Using Single Pattern

The very first method to exclude the described pattern from the file is using the ā€œ-vā€ flag within the ā€œgrepā€ instruction is the easiest and simple one. In this command, we will be displaying all the contents of a file using ā€œcatā€ instruction and exclude those lines of text which are matched from the defined one. The grep and cat command has been separated by a separator line. So, we have been using the pattern ā€œCSSā€ in the query. All the lines which contain the pattern ā€œCSSā€ within them would be excluded from the output data. Thus, all the remaining lines will be displayed on the shell. The output shows that there is no line in the resultant data containing the pattern ā€œCSSā€. The command is displayed in the image.

$ cat new.txt | grep ā€“v ā€œCSSā€

Another way to use the same grep command is without the ā€œcatā€ instruction. In this way, you have to only mention the pattern within inverted commas after the flag ā€œ-vā€ and add the file name after it. The grep command will exclude the matched pattern lines and display the remaining ones in the shell. The output is as expected as per the image beneath.

$ grep ā€“v ā€œCSSā€ new.txt

Letā€™s use another excluding pattern in the grep command to exclude the lines. So, we have used the string ā€œisā€ instead of ā€œCSSā€ this time. As the word ā€œisā€ is used a lot in the file, it excluded all the 4 lines containing the word ā€œisā€ in the output. Thus, only 2 lines remained to be displayed on the shell.

$ grep ā€“v ā€œisā€ new.txt

Letā€™s see how the grep command works on the new excluding pattern this time. So, we have utilized the pattern ā€œeā€ in the command to be excluded. The output shows nothing. This demonstrates that the pattern has been found in every line of the file as we know that the alphabet ā€œeā€ has been most used in words. Hence, there is nothing left to be displayed on the console from the file new.txt.

$ grep ā€“v ā€œeā€ new.txt

Exclude Using Multiple Patterns

The above examples illustrate excluding texts from the files with a single pattern mentioned in the command. Now, we will be using the multiple patterns in the same syntax of commands to see how it works. So, we have used the very first syntax of the grep command to exclude the lines from the file ā€œnew.txtā€ and display the remaining lines. We have used the 2 patterns to be searched and then excluded from the file, i.e., ā€œCSSā€ and ā€œisā€. The patterns have been defined with the flag ā€œ-eā€ separately. As the 5 lines of the new.txt file contain both the patterns, it only displays the remaining 1 line in the terminal as displayed.

$ cat new.txt | grep ā€“v -e ā€œCSSā€ ā€“e ā€œisā€

Letā€™s use the other syntax of the grep query in the shell to exclude the matching patterns or related lines while using the multiple patterns. So, we have been using the pattern ā€œtextā€ and ā€œisā€ in the command to exclude the lines from the file ā€œnew.txtā€. The output of this query displays the single line left that has no word matched with the specified pattern.

$ grep ā€“v ā€“e ā€œtextā€ ā€“e ā€œisā€ new.txt

There is another unique way to exclude the multiple patterns from the file using the grep command. The command is almost the alike with a slight change. You have to add the alphabet ā€œEā€ with the flag ā€œ-vā€. After that, you have to add the multiple patterns to be excluded within the inverted commas separated by a separator line. The example command is shown below. We searched for the patterns ā€œtā€ and ā€œkā€ from the file new.txt to exclude the lines containing these patterns. In return, we have left with only 3 lines that are displayed in the image.

$ grep ā€“Ev ā€œt|kā€ new.txt

Exclude Using Case Sensitive Flag

Like the ā€œ-vā€ flag, you can also use a case-sensitive flag in the grep command to exclude the pattern. It will work similarly as it works for the ā€œ-vā€ flag but with more accuracy. You may use it as per your desire. So, we have been using the ā€œ-Iā€ flag with the ā€œ-vā€ flag in the command. To search for the pattern ā€œtextā€ in the file ā€œnew.txtā€. This file contains a line having the string ā€œtextā€ in it as a whole. Hence, the whole line has been excluded from the file using the command below.

$ grep ā€“I ā€“v ā€“E ā€œtextā€ new.txt

Letā€™s use another file to exclude patterns from it. The data of this file has been displayed below.

$ cat test.txt

Letā€™s use the same case-sensitive flag command to exclude the lines that contain the pattern ā€œtextā€ in the file. In return, the texted lines have been removed, and only the dotted lines are left displayed.

$ grep ā€“I ā€“v ā€“E ā€œtextā€ test.txt

Conclusion

This article contains different ways to use the Linux grep command to exclude matching patterns from the files. We elaborated several examples to clarify the concept of grep to exclude matches. We hope that you will find this article great while exploring the ā€œgrepā€ exclude pattern command in Linux.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.