Grep Exact Match - Use Regex

Grep is a Linux command-line tool used to search for a specific string or text in the file.

You can use it with a regular expression to be more flexible at finding strings. You can also use the grep command to find only those lines that completely match the search string.

In this tutorial, we will show you how to search for an Exact Pattern with some practical examples.

Create a Sample File

First, let’s create a sample file to explain all the examples with hands-on practical.

cat file.txt

You should see the following lines:

webservertalk
webservertalk is a linux blog
This is a webservertalk website
This is a my20webservertalk website
Here is a 20.webservertalk.20 website
There is a 1234webservertalk website
what is a(webservertalk)b website
Here is a 22webservertalk22 website

Basic Search with Grep

Basically, the grep command consists of three parts including:

  1. grep command,
  2. search string,
  3. and file name

In the following example, we will search a word webservertalk from a file.txt.

You can use -w option to search a specific word:

grep -w webservertalk file.txt

You should get the following output:

webservertalk
webservertalk is a linux blog
This is a webservertalk website
Here is a 20.webservertalk.20 website
what is a(webservertalk)b website

As you can see, the above command prints all lines containing word webservertalk. It does not perform well if you want to print an exact match of the whole word and remove non-relevant matches.

Grep to Match First and Last Character.

You can also use the grep command to find an exact match by using the beginning(^) and ending($) character.

Let’s run the following command to understand it better:

grep -E "^webservertalk$" file.txt

You should get the following output:

webservertalk


As you can see, the above command is unable to print all lines that contain the word “ webservertalk“. That means this command does not work if you want to find the whole word in the middle of the line.

Grep to Match String with White Space

In order to print the exact match, you will need to search for a word with leading or trailing white space characters.

Let’s search for a line that matches the exact word webservertalk.

grep -E "(^| )webservertalk( |$)" file.txt

You should get the following output:

webservertalk
webservertalk is a linux blog
This is a webservertalk website

Grep to Match Beginning and End of Word

You can use grep extended regex to match the begin and end of the word. In the following example, we will search for a word webservertalk , use (\s|$) to search a specified word ending with white space and use /b to match the empty string at the edge of a word.

grep -E "\bwebservertalk(\s|$)" file.txt

You should see the following output:

webservertalk
webservertalk is a linux blog
This is a webservertalk website

Grep to Match Numbers in the String

In this example, we will search for a word 1234webservertalk from a file.txt. As you can see the word 1234webservertalk contains numbers at the starting of the word.

In that case, we will use [0-9]+ to search for a word that contains integers at the starting of the word.

grep -E "[0-9]+webservertalk( |$)" file.txt

You should get the following output:

This is a my20webservertalk website
There is a 1234webservertalk website

Conclusion

In the above tutorial, we explained how to search the exact matching word from a file using the grep command. We hope you have now enough knowledge on the grep command-line to find the exact matching word.

Leave a Reply

Your email address will not be published. Required fields are marked *