Exact Match with Grep Command

The grep command is the very popular command-line tool to match or grep given pattern in the specified text or content. One of the most popular cases for the grep command is the exact match. This can be also called an exact string match with the grep command.

Exact Match with -w Option

The -w option is used to match specified term exactly for the given content. Actually the -w option is created to match words where single word can match. But the -w option can be also used to match exatly for the specified term. In the following example it matches the “linuxtect.com”.

grep -w "linuxtect.com" file.txt

Exact Match with Regular Expression

The grep is a powerful tool that also supports regular expressions. Regular expressions can be used to match exactly for the given search term. The \< and \> are used to match as a whole for the string specified inside them. In this case, we do not need to specify the -w option as we implement the exact match with the regular expression.

grep "\<linuxtect.com\>" file.txt

Exact Match For Start Of Line

The exact match can be also used for the start of the line the characters. The ^ sign is used to specify the start of the line. The term we want to exact match is added after the start of the line sign.

grep "^Linuxtect.com" file.txt

Exact Match For End Of Line

Another way for the exact match with the grep is matching the end of the line. The $ sign is used to specify the end of the line.

grep "linuxtect.com$" file.txt

Leave a Comment