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.
grep -w "word" file.txtThe -w option ensures that grep matches the whole word only, preventing partial matches inside other words.
For example:
grep -w "error" logfile.txtThis command will match:
errorBut it will ignore partial matches such as:
errors
myerrorlogSample file used in examples
To demonstrate the examples in this tutorial, consider the following sample file:
cat /tmp/somefileExample content:
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
abcdWe 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:
grep -w "abcd" /tmp/somefileExample output:
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcdHere, grep matches occurrences where abcd appears as a standalone word.
It ignores cases such as:
abcd123
123abcdbecause 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.
grep -iw "abcd" /tmp/somefileThis command matches both uppercase and lowercase versions of the word.
Example matches may include:
ABCD
abcd
Abcdgrep exact match in multiple files
You can also search for an exact word across multiple files using grep.
grep -w "error" *.logThis 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:
grep -rw "error" /var/logThis 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.
grep -E "abcd" /tmp/somefileThis command searches for the string abcd anywhere in the line.
Example matches may include:
abcd some text
second line abcd some text
1234abcdBecause 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.
grep -E "\babcd\b" /tmp/somefileHere:
\brepresents a word boundary- The pattern ensures that
abcdis matched as a complete word
Example matches:
second line abcd some text
abcd some text
abcdThis prevents matching strings such as:
abcd123
myabcdfileMatch exact string with whitespace boundaries
Another approach is to explicitly match whitespace or line boundaries before and after the pattern.
grep -E "(^| )abcd( |$)" /tmp/somefileExplanation:
(^| )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:
second line abcd some text
abcd some text
abcdThis 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:
grep "^abcd$" /tmp/somefileExample output:
abcdThis command ensures that only lines where abcd appears alone are matched. It will ignore lines such as:
abcd some text
text abcd
123abcdbecause 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:
grep -E "\babcd\b" /tmp/somefileExample output:
second line abcd some text
abcd some text
abcdThis command prevents matching patterns such as:
abcd123
myabcdfilebecause 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.
grep -E "(^| )abcd( |$)" /tmp/somefileExplanation:
(^| )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:
second line abcd some text
abcd some text
abcdThis 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:
grep -E "[0-9]+abcd( |$)" /tmp/somefileExplanation:
[0-9]+matches one or more digitsabcdis the text pattern( |$)ensures the match ends with either whitespace or the end of the line
Example output:
sixth line 1234abcd some textThis 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:
grep -E "[0-9]+abcd\s" /tmp/somefileRegex groups allow you to build flexible matching rules while still controlling how exact patterns appear in text.
Methods to grep Exact Match (Quick Comparison)
| Method | Command | Use Case |
|---|---|---|
| Whole word | grep -w "word" file | Most common exact match |
| Word boundary | grep -E "\bword\b" file | Strict word boundary match |
| Exact line | grep "^word$" file | Match entire line only |
| Regex pattern | grep -E "pattern" file | Complex 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.
grep -w "word" file.txt
grep -iw "word" file.txt
grep "^word$" file.txt
grep -E "\bword\b" file.txtThese 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:
- grep command examples in Linux (Cheat Sheet)
- grep multiple patterns in Linux
- grep recursive search examples
These tutorials provide additional examples for working with grep in real-world Linux command line scenarios.





![Print Next Word Before or After Pattern Match [SOLVED]](/print-next-word-after-pattern-match-linux/shell_scripting_hu_9d40fc0e7de305df.webp)