How to Exclude Matches with grep

3

Grep stands for “global regular expression print” has been a extensively used command within the Linux platform. It has been applied to do a large number of purposes whilst the usage of other flag key phrases. The grep command with “-v” can be utilized in different techniques to exclude the fits from the recordsdata. Let’s have a pointy have a look at each and every one in every of them separately. Firstly you will have to have some textual content document to accomplish the grep command on it. So we’ve got been the usage of the “new.txt” document that exists in our house listing. Thus, we need to use the “cat” question within the console to show the knowledge of this document. The document incorporates a complete of 6 strains, as illustrated within the symbol.

Exclude Using Single Pattern

The first actual technique to exclude the described development from the document is the usage of the “-v” flag throughout the “grep” instruction is the very best and easy one. In this command, we will be able to be exhibiting all of the contents of a document the usage of “cat” instruction and exclude the ones strains of textual content that are matched from the outlined one. The grep and cat command has been separated by means of a separator line. So, we’ve got been the usage of the development “CSS” within the question. All the strains which comprise the development “CSS” inside of them can be excluded from the output information. Thus, all of the closing strains can be displayed at the shell. The output displays that there is not any line within the resultant information containing the development “CSS”. The command is displayed within the symbol.

$ cat new.txt | grep –v “CSS”

How to Exclude Matches with grep How to Exclude Matches with grep 1634017671 386 How to Exclude Matches with grep

Another means to make use of the similar grep command is with out the “cat” instruction. In this manner, you need to handiest point out the development inside of inverted commas after the flag “-v” and upload the document title after it. The grep command will exclude the matched development strains and show the rest ones within the shell. The output is as anticipated as in line with the picture underneath.

$ grep –v “CSS” new.txt

How to Exclude Matches with grep How to Exclude Matches with grep 1634017672 502 How to Exclude Matches with grep

Let’s use every other apart from development within the grep command to exclude the strains. So, we’ve got used the string “is” as a substitute of “CSS” this time. As the phrase “is” is used so much within the document, it excluded all of the 4 strains containing the phrase “is” within the output. Thus, handiest 2 strains remained to be displayed at the shell.

$ grep –v “is” new.txt

How to Exclude Matches with grep How to Exclude Matches with grep 1634017673 997 How to Exclude Matches with grep

Let’s see how the grep command works at the new apart from development this time. So, we’ve got applied the development “e” within the command to be excluded. The output displays not anything. This demonstrates that the development has been present in each line of the document as we all know that the alphabet “e” has been maximum utilized in phrases. Hence, there may be not anything left to be displayed at the console from the document new.txt.

$ grep –v “e” new.txt

How to Exclude Matches with grep How to Exclude Matches with grep 1634017673 468 How to Exclude Matches with grep

Exclude Using Multiple Patterns

The above examples illustrate apart from texts from the recordsdata with a unmarried development discussed within the command. Now, we will be able to be the usage of the more than one patterns in the similar syntax of instructions to look the way it works. So, we’ve got used the first actual syntax of the grep command to exclude the strains from the document “new.txt” and show the rest strains. We have used the two patterns to be searched after which excluded from the document, i.e., “CSS” and “is”. The patterns were outlined with the flag “-e” one by one. As the 5 strains of the brand new.txt document comprise each the patterns, it handiest shows the rest 1 line within the terminal as displayed.

$ cat new.txt | grep –v -e “CSS” –e “is”

How to Exclude Matches with grep How to Exclude Matches with grep 1634017673 536 How to Exclude Matches with grep

Let’s use the opposite syntax of the grep question within the shell to exclude the matching patterns or similar strains whilst the usage of the more than one patterns. So, we’ve got been the usage of the development “text” and “is” within the command to exclude the strains from the document “new.txt”. The output of this question shows the only line left that has no phrase matched with the required development.

$ grep –v –e “text” –e “is” new.txt

How to Exclude Matches with grep How to Exclude Matches with grep 1634017673 704 How to Exclude Matches with grep

There is every other distinctive method to exclude the more than one patterns from the document the usage of the grep command. The command is nearly the alike with a slight alternate. You have so as to add the alphabet “E” with the flag “-v”. After that, you need to upload the more than one patterns to be excluded throughout the inverted commas separated by means of a separator line. The instance command is proven under. We looked for the patterns “t” and “k” from the document new.txt to exclude the strains containing those patterns. In go back, we’ve got left with handiest 3 strains which can be displayed within the symbol.

$ grep –Ev “t|k” new.txt

How to Exclude Matches with grep How to Exclude Matches with grep 1634017674 691 How to Exclude Matches with grep

Exclude Using Case Sensitive Flag

Like the “-v” flag, you’ll additionally use a case-sensitive flag within the grep command to exclude the development. It will paintings in a similar fashion as it really works for the “-v” flag however with extra accuracy. You would possibly use it as in line with your want. So, we’ve got been the usage of the “-I” flag with the “-v” flag within the command. To seek for the development “text” within the document “new.txt”. This document incorporates a line having the string “text” in it as an entire. Hence, the entire line has been excluded from the document the usage of the command under.

$ grep –I –v –E “text” new.txt

How to Exclude Matches with grep How to Exclude Matches with grep 1634017674 262 How to Exclude Matches with grep

Let’s use every other document to exclude patterns from it. The information of this document has been displayed under.

How to Exclude Matches with grep How to Exclude Matches with grep 1634017675 518 How to Exclude Matches with grep

Let’s use the similar case-sensitive flag command to exclude the strains that comprise the development “text” within the document. In go back, the texted strains were got rid of, and handiest the dotted strains are left displayed.

$ grep –I –v –E “text” take a look at.txt

How to Exclude Matches with grep How to Exclude Matches with grep 1634017675 435 How to Exclude Matches with grep

Conclusion

This article incorporates other ways to make use of the Linux grep command to exclude matching patterns from the recordsdata. We elaborated a number of examples to explain the concept that of grep to exclude fits. We hope that you’re going to to find this newsletter nice whilst exploring the “grep” exclude development command in Linux.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More