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.