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.
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.
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.
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.
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.
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.
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.
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.
Letās use another file to exclude patterns from it. The data of this file has been displayed below.
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.
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.