Global regular expression print is a versatile terminal-based utility. As the name shows that it helps in searching the text within the file with the help of regular expressions. Grep is firstly originated as a Unix utility to run on that operating platform. After Linux configuration, it can access many applications on this operating system. Most Grep functions are included in the matching of the text of the file present in the command. Exclude function is also as useful as matching any pattern and displaying it because it helps remove the particular match from the file. It helps to exclude the word or words from the lines in a file. We can get help from the man page in the system by applying the below-appended command.

We have found two important keywords used in excluding terms in any file. –v is used to invert the match; it then outputs the nonmatching lines in the text.

Prerequisite

To perform the functionality, we need to have Linux installed in our system configured on the virtual machine. By adding a username and password,  you will have access to applications in the operating system. You need a terminal to open and run commands on it.

Example 1

To apply this function to a word, we need to have a file existing in our system. If you don’t have any files, then create them first. We have a file named fileb.txt. We will use the cat command to display text.

This image shows the output of the file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image1-18.png" data-lazy- height="182" src="data:image/svg xml,” width=”473″>

If we want to exclude some words from the text, we will use the following command to exclude the words in the fileb.txt

$ grep –I –v –E ‘ubuntu’ fileb.txt

In the command above, we have used –v that will invert the text in the query. Ubuntu is the word we want to exclude from the given text. –I is for case sensitivity and an optional thing that is if the desired output is to obtained without using –i. “|” is used to exclude or match the accurate words. The output of this command is to be appended below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image3-15.png" data-lazy- height="156" src="data:image/svg xml,” width=”694″>

In this output, you have seen that “ubuntu” is removed from the file. To draw another word, say Linux, from the file, we can amend the given command.

$ grep –I –v –E ‘Ubuntu|Linux’ fileb.txt

In this way, at a time together, more than one word will be excluded.

Example 2

In this example, the whole string is removed from the file. The target word is mentioned in the command, and the command works in such a way that word is matched with the text in a string, and in this way, the whole string is removed from the file. Syntax of command is the same as described above in this guide. Let us have a file named file22.txt. Firstly, we will display all of the contents so that the respective result will show the difference.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image2-15.png" data-lazy- height="171" src="data:image/svg xml,” width=”571″>

Now we will apply the command to exclude the whole string from the file.

$ grep –v ‘technical’ file22.txt

The command will be applied in such a way that it will match the target word and will display all the strings except the one that contains the match. Now you can see that the first string is not present in the text file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image5-13.png" data-lazy- height="154" src="data:image/svg xml,” width=”621″>

Exclude Term of Multiple Words

Unlike the examples above, here we will mention more than one command to exclude them from the text file. Cat and Grep both act in the same way. Now with the help of the given command, we will understand this concept.

$ cat file20.txt | grep –v –e “good” –e “years”

$ grep –v –e “good” –e “years” file20.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image4-14.png" data-lazy- height="42" src="data:image/svg xml,” width=”719″>

In this command, –e is used for more than one term as an input in the command. It will eliminate both words from the text. The first command implies the file to be displayed and then remove the words we want to exclude. Simultaneously, the second command will use –v first to remove the words written further in the command.

Here is another way of exclusion. Firstly, we exclude one word by providing a file address, and after “|” we will introduce the second word.

$ grep –v “years” file20.txt  | grep “good”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image7-11.png" data-lazy- height="23" src="data:image/svg xml,” width=”668″>

Exclude File

Like words, we can also exclude the file from the system. We will use the following command.

$ grep – exclude “file21.txt” grep *.txt

This command will remove the file. This command will use the “—exclude” keyword to remove the file. “*.txt” implies that the file is a “txt” extension. Command will work on all the text files to search the relevant file that is present in the system.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image6-11.png" data-lazy- height="22" src="data:image/svg xml,” width=”688″>

Exclude Directory with Word

The directory can also be excluded by defining a word. This command will help to match the word present in any text file of a directory and then remove the respective directory or directories having that word in it. Here,  we don’t mention the file name in the command.

$ grep-exclude-dir “good” –R “grep

“dir” represents the directory in the system. –R shows the recursive function. To do any alteration in directories, we always use –R.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image9-10.png" data-lazy- height="25" src="data:image/svg xml,” width=”684″>

We will quote another example that shows that directories that contain the word “Aqsa” are removed from the system.

$ grep-exclude-dir “directory” –R “aqsa”

It will show all directories, including the word Aqsa.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image8-10.png" data-lazy- height="197" src="data:image/svg xml,” width=”719″>

Exclude word with Help of Directory

As we have excluded the directory by using the word, we can also exclude the word using the directory or provide the whole path of the file.

$ grep –R “years” /home/aqsayasin/file20.txt/ | grep –v “exclude this”

In this command, we want to exclude the word year. To introduce the directory, we will write –R. Consider file20.txt as below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image12-7.png" data-lazy- height="84" src="data:image/svg xml,” width=”718″>

Now apply the following command by using the directory as input.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image10-9.png" data-lazy- height="40" src="data:image/svg xml,” width=”717″>

The output obtained from this command will exclude the word year from the output.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image11-7.png" data-lazy- height="57" src="data:image/svg xml,” width=”715″>

Moving towards another example. Here, we will exclude the word “grep” from the directory using the following appended command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/image13-5.png" data-lazy- height="245" src="data:image/svg xml,” width=”712″>

Conclusion

Excluding term is an alternative to the matching process of Grep. It helps in removing unwanted words or strings from the files present in the system. This article will assist you in getting rid of unwanted words.

About the author

<img alt="Aqsa Yasin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/04/echo/Author-Image-150×150.jpg607e25cca0490.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.