
As you can see the word 1234webservertalk contains numbers at the starting of the word. In this example, we will search for a word 1234webservertalk from a file.txt. 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. You can use grep extended regex to match the begin and end of the word. Grep -E "(^| )webservertalk( |$)" file.txt Let’s search for a line that matches the exact word webservertalk.

In order to print the exact match, you will need to search for a word with leading or trailing white space characters. That means this command does not work if you want to find the whole word in the middle of the line. Let’s run the following command to understand it better:Īs you can see, the above command is unable to print all lines that contain the word “ webservertalk“. You can also use the grep command to find an exact match by using the beginning(^) and ending($) character. It does not perform well if you want to print an exact match of the whole word and remove non-relevant matches. You can use -w option to search a specific word:Īs you can see, the above command prints all lines containing word webservertalk. In the following example, we will search a word webservertalk from a file.txt.

Create a Sample Fileįirst, let’s create a sample file to explain all the examples with hands-on practical.īasically, the grep command consists of three parts including:
#Grep find word in file how to#
In this tutorial, we will show you how to search for an Exact Pattern with some practical examples. You can also use the grep command to find only those lines that completely match the search string. You can use it with a regular expression to be more flexible at finding strings. Grep is a Linux command-line tool used to search for a specific string or text in the file.
