The grep, or global regular expression print, works on the command line interface. It is one of the most used commands in Linux that allows the user to find or sort data according to syntax matches. However, if you want to exclude different words, patterns, or files, there is a slight change in how you use the command.
The grep command is used widely by programmers and developers to search a code snippet or exclude the name of the variables they wish not to see. It is relatively easy to use because there is no chance of altering the code, which often happens while reviewing the code.
This article will guide the users to exclude keywords, lines, or files in grep to retrieve data or text efficiently. The content of the post is as follows:
- What is grep in Linux?
- How to Exclude in grep?
- Example 1: How to Exclude a Single Word Using the grep?
- Example 2: How to Exclude Multiple Strings or Words Using grep?
- Example 3: How to Exclude the First Word From a Line Using grep?
What is grep in Linux?
Before using grep, make sure you have it installed on your system, although it is pre-installed most of the time. Execute this command to get information about grep in the terminal:
$ grep --version
However, if you don’t find it installed, then you must use this command to install it:
$ sudo apt-get install -y grep
The above command is used to install grep on Debian based system if you want to install it on Arch Linux or Manjaro then you can do that by typing:
$ sudo pacman -Sy yaourt $ sudo yaourt grep
For Fedora, you can type:
$ sudo dnf install snapd $ sudo snap install grep-nsantos
For CentOS you can type:
$ sudo yum install grep
Now, you may be wondering what the options are, right? Well, here is a list of them.
|-w||Shows the lines with strings (only complete words)|
|-l||To specify case sensitivity|
|-r||For recursive search|
|-v||Shows the lines which aren’t a match to searched strings|
|–exclude-dir||Excludes the directories from the search|
|-e||To declare multiple keywords or pattern|
Looking at the above table, we can see many commands, but we will only cover the text exclusion methods.
How to Exclude in grep?
Once you have good control over the grep command in Linux, you can use and exclude in grep. For a better understanding, the following examples are practiced to exclude in grep.
Example 1: How to Exclude a Single Word Using the grep?
To exclude the words, we’d need a text file, which can be any file with text in it that can be accessed using the “cat command” below:
$ cat test
Now, to exclude the text, which in our case is “it”; let’s see how it works by executing this command using a ‘-v’ flag.
$ grep -v “it” test
You can compare the two images right above, and you will see that in the second image, no line had the word “it” in it.
Example 2: How to Exclude Multiple Strings or Words Using grep?
Earlier, we excluded a single text or string from a text file, but you can also exclude multiple strings or words. For instance, by executing this command.
$ sudo grep -v -e “it” -e “is” test
The output shows that all the lines that contain the words “it” and “is” are excluded.
Example 3: How to Exclude the First Word From a Line Using grep?
Using the grep utility, the first (selected) words from a line can be excluded.
It can also be somehow used to get rid of the words which are being repeated and again. The syntax for these scenarios is described below.
$ grep -v “^string to exclude” filename,
For instance, the command provided below will remove the line that starts with the keyword “Linux”:
$ grep -v “^Linux” test
It is observed that the line containing the word “Linux” has been deleted.
That’s all from this guide!
The grep command line utility offers multiple options, such as “-v” and regular expressions to exclude the specific lines from the search results. The grep command is relatively easy and has different variations, making it an excellent command for excluding the string(s) or word(s) from a text file. This post has demonstrated a detailed method to exclude in grep.