3 ways to exact match using grep

In this Linux, basic command tutorial will see 3 ways to exact match with grep command.

The grep command, can search a whole line, exact match, exclude the pattern and search multiple words.

Mainly in Industries grep command is used to find the word in a file or specific error on the logs.

If you want to learn more about the grep command you can refer to the given link.

Let’s discuss the 3 ways to match the word using the grep command,

  1. use the grep command with the -w option.
  2. grep command to match the Numbers in a String.
  3. grep to match the first and the last character.

Use the grep command with the -w option to match the word.

Let’s use the -w option to search the exact word from the text file.

we are going to search the word “Mono” in the file name tastethelinux.txt

# grep -w Mono tastethelinux.txt

Now, let’s match the exact word on the current directory. Have 4 files named “grep_example”, “Linux_OS”, “Security_OS”, and “tastethelinux”.

Also, we are in the current directory then use * to search the word “Mono” in all files.

# grep -w Mono *
grep exact match command in Linux and Unix

Match the Numbers in a String with grep Command.

What did mean to match the Numbers that are in the string?

Let’s search the word tastethelinux, but it contains Numbers like “2022tastethelinux”, “year2020tastethelinux” or “tastethelinuxin2021”.

So for that, use the -E option for the extended regular expressions. Already identified the numbers that are with the strings.

So there is a file named “grep_example.txt” that has the below content.

# cat grep_example.txt 
OUTPUT :

This is a Linux tutorial
This is Linux grep Command
grep is a powerfull tool
First example for grep with numbers is 2022tastethelinux
Second example for grep with numbers is year2020tastethelinux
Third example for grep with numbers is tastethelinuxin2021
Have a nice Day!!!

By using the -E option we are going to search a above highlighted words. So in regular expressions

# grep -E "[0-9]+tastethelinux" grep_example.txt
OUTPUT :

First example for grep with numbers is 2022tastethelinux
Second example for grep with numbers is year2022tastethelinux

Let’s move to another example which has the numbers at the end of the String.

# grep -E "tastethelinux[0-9]+" grep_example.txt
OUTPUT :

Third example for grep with numbers is tastethelinux2021

Match the first and the last character.

Now let’s see an example of “how to match the first and last character using grep command.

For this example also going to use the -E option, with the grep command.

So to match the first character use the symbol ^(circumflex) and $(dollar) for the end character.

Let’s match the word “Tastethelinux” on a text file named “grep_example.txt”

# cat grep_example.txt 
OUTPUT:

Tastethelinux
Welcome to Tastethelinux
Have a nice Day!!!
# grep -E "^Tastethelinux$" grep_example.txt 
OUTPUT:

Tastethelinux

The above example has only printed the first line, What if we have to match the word for the end character.

# grep -E "Tastethelinux$" grep_example.txt 
OUTPUT:

Tastethelinux
Welcome to Tastethelinux

So only used $(dollar) symbol for the end character.

If you want to learn more about grep command, you can refer to man page of this.


Give your valuable time