grep Exact Match in Linux (Match Whole Word or Exact String)

grep Exact Match in Linux (Match Whole Word or Exact String)

grep Exact Match in Linux

The grep command is commonly used in Linux to search for text patterns inside files. By default, grep returns any line that contains the specified pattern, even if the pattern appears as part of another word.

Sometimes you may want to search for an exact match, meaning the word or string must appear exactly as specified without matching partial words or unintended patterns. This can be done using different grep options or regular expressions.

In this guide, you will learn several ways to perform an exact match using the grep command in Linux.

Quick command to grep exact match

The easiest way to search for an exact word using grep is with the -w option.

text
grep -w "word" file.txt

The -w option ensures that grep matches the whole word only, preventing partial matches inside other words.

For example:

text
grep -w "error" logfile.txt

This command will match:

text
error

But it will ignore partial matches such as:

text
errors
myerrorlog

Sample file used in examples

To demonstrate the examples in this tutorial, consider the following sample file:

text
cat /tmp/somefile

Example content:

text
first line ABCD some text
second line abcd some text
third line12abcde some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
sixth line 1234abcd some text
seventh line 1abcd234 some text
eighth line 234abcd1 some text
abcd some text
abcd

We will use this file in the examples below to demonstrate how different grep commands behave when matching exact patterns.


Method 1: grep Exact Word using -w (Most Common Method)

The most common way to perform an exact match using grep is by using the -w option. This option ensures that the match occurs only when the pattern appears as a complete word.

grep exact word in a file

To search for an exact word inside a file:

text
grep -w "abcd" /tmp/somefile

Example output:

text
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd

Here, grep matches occurrences where abcd appears as a standalone word.

It ignores cases such as:

text
abcd123
123abcd

because the pattern appears as part of another word.

grep exact word ignoring case

Sometimes the word may appear in different letter cases. In such cases, you can combine the -w option with -i to perform a case-insensitive exact match.

text
grep -iw "abcd" /tmp/somefile

This command matches both uppercase and lowercase versions of the word.

Example matches may include:

text
ABCD
abcd
Abcd

grep exact match in multiple files

You can also search for an exact word across multiple files using grep.

text
grep -w "error" *.log

This command searches all .log files in the current directory and returns lines containing the exact word error.

If you want to recursively search across directories:

text
grep -rw "error" /var/log

This searches all files inside /var/log and its subdirectories for the exact word error.


Method 2: grep Exact String using Regex

In some situations, the -w option may not provide the level of control required. You can use regular expressions (regex) with grep to perform stricter exact matching.

Match exact string anywhere in a line

You can match an exact string using extended regular expressions.

text
grep -E "abcd" /tmp/somefile

This command searches for the string abcd anywhere in the line.

Example matches may include:

text
abcd some text
second line abcd some text
1234abcd

Because no boundaries are defined, grep matches the string wherever it appears.

Match exact string with word boundaries

Word boundaries ensure that the match occurs only at the edge of a word.

text
grep -E "\babcd\b" /tmp/somefile

Here:

  • \b represents a word boundary
  • The pattern ensures that abcd is matched as a complete word

Example matches:

text
second line abcd some text
abcd some text
abcd

This prevents matching strings such as:

text
abcd123
myabcdfile

Match exact string with whitespace boundaries

Another approach is to explicitly match whitespace or line boundaries before and after the pattern.

text
grep -E "(^| )abcd( |$)" /tmp/somefile

Explanation:

  • (^| ) ensures the word starts either at the beginning of the line or after a space
  • ( |$) ensures the word ends either with a space or at the end of the line

Example output:

text
second line abcd some text
abcd some text
abcd

This technique is useful when you need precise control over pattern boundaries using regular expressions.


Method 3: grep Exact Line Match

Sometimes you may want to match an entire line exactly, rather than matching a word somewhere inside a line. In such cases, regular expression anchors can be used to ensure the pattern matches the beginning and end of the line.

Match an entire line using ^ and $

The ^ and $ symbols represent the start and end of a line in regular expressions.

  • ^ indicates the beginning of the line
  • $ indicates the end of the line

To match a line that contains only the word abcd, use:

text
grep "^abcd$" /tmp/somefile

Example output:

text
abcd

This command ensures that only lines where abcd appears alone are matched. It will ignore lines such as:

text
abcd some text
text abcd
123abcd

because the pattern must match the entire line exactly.


Method 4: grep Strict Match using Word Boundaries

In some cases, you may want stricter matching rules than the -w option. Word boundaries help ensure that the pattern appears as a separate word and is not part of a larger string.

Using \b word boundary

The \b character represents a word boundary in regular expressions. It matches the position between a word character and a non-word character.

To match abcd only as a complete word:

text
grep -E "\babcd\b" /tmp/somefile

Example output:

text
second line abcd some text
abcd some text
abcd

This command prevents matching patterns such as:

text
abcd123
myabcdfile

because the word must be bounded by non-word characters.

Using start and end delimiters

Another way to perform strict matching is by defining the boundaries explicitly using start-of-line or whitespace delimiters.

text
grep -E "(^| )abcd( |$)" /tmp/somefile

Explanation:

  • (^| ) ensures the word starts either at the beginning of the line or after a space
  • ( |$) ensures the word ends either with a space or at the end of the line

Example output:

text
second line abcd some text
abcd some text
abcd

This approach provides strict control over pattern boundaries when searching text.


Method 5: grep Exact Match with Numbers or Patterns

Sometimes the exact match may involve digits or patterns combined with text. Regular expressions allow you to match such combinations precisely.

Match digits before text

If you want to match a pattern where numbers appear before a word, you can use a numeric character class.

Example:

text
grep -E "[0-9]+abcd( |$)" /tmp/somefile

Explanation:

  • [0-9]+ matches one or more digits
  • abcd is the text pattern
  • ( |$) ensures the match ends with either whitespace or the end of the line

Example output:

text
sixth line 1234abcd some text

This allows matching patterns where numbers appear directly before the word.

Match exact pattern with regex groups

You can also combine groups and boundaries to match more complex patterns.

For example, to match abcd only when it appears after a number and before whitespace:

text
grep -E "[0-9]+abcd\s" /tmp/somefile

Regex groups allow you to build flexible matching rules while still controlling how exact patterns appear in text.


Methods to grep Exact Match (Quick Comparison)

MethodCommandUse Case
Whole wordgrep -w "word" fileMost common exact match
Word boundarygrep -E "\bword\b" fileStrict word boundary match
Exact linegrep "^word$" fileMatch entire line only
Regex patterngrep -E "pattern" fileComplex or custom pattern matching

Frequently Asked Questions

1. How do I grep an exact match in Linux?

Use the -w option with grep to match an exact word. For example: grep -w "word" file.txt. This ensures that only whole word matches are returned.

2. How do I grep exact word in Linux?

Use grep with the -w option. Example: grep -w "error" logfile.txt. This matches the exact word without matching partial strings.

3. How do I match an exact line using grep?

Use start and end anchors in regex. Example: grep "^word$" file.txt. This matches lines that contain only the exact word.

4. How do I grep exact match ignoring case?

Use the -iw option with grep. Example: grep -iw "word" file.txt. This matches the exact word regardless of case.

5. What does grep -w do?

The -w option tells grep to match only whole words. It prevents partial matches inside larger words.

Summary

Searching for an exact match using the grep command in Linux can be done using several options depending on the type of match required. Whether you want to match a whole word, an exact line, or a strict pattern using regular expressions, grep provides flexible ways to control how text is matched.

The most appropriate method depends on the scenario. For simple word matching, the built-in options of grep are usually sufficient, while regular expressions are helpful for more advanced matching rules.

Most common grep exact match commands

Below are some of the most frequently used commands to perform exact matches using grep.

text
grep -w "word" file.txt
grep -iw "word" file.txt
grep "^word$" file.txt
grep -E "\bword\b" file.txt

These commands allow you to:

  • match an exact word
  • perform case-insensitive exact matches
  • match an entire line exactly
  • enforce strict word boundaries

Each method can be useful depending on how the pattern appears inside the file.

Best method to match exact words

In most situations, the easiest and most reliable way to match an exact word using grep is the -w option.

grep -w "word" file.txt

This method ensures that the pattern is matched only when it appears as a complete word, preventing partial matches inside other strings.

For more complex matching scenarios, regular expressions such as \bword\b or start-and-end anchors like ^word$ can be used to define stricter boundaries around the pattern.


Further Reading

If you want to explore more examples and advanced usage of the grep command, the following guides may be helpful:

These tutorials provide additional examples for working with grep in real-world Linux command line scenarios.

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.