copied to clipboard!
string grep

Mastering Exact Match in grep: A Complete Guide

updated: 2026/07/21 created: 2026/07/21

Introduction

grep is a well-known command for searching strings on Linux and Unix systems.

However, the difference between partial match and exact match is often confusing, and results don't always come out as expected.

This article explains everything in detail, from the basics to more advanced usage.

Reference: GNU grep

Basic Syntax for grep Exact Match

Create File

cat << 'EOF' > input.txt apple banana orange apple pie Apple apple EOF

Command

grep -x "apple" input.txt

Result

apple
apple

Command

grep -Fx "apple" input.txt

Result

apple
apple

How It Works

Item Description
grep Command that searches for a specified string
-x Displays only lines where the entire line exactly matches the search string
-F Treats the search string as a fixed string rather than a regular expression
"apple" The string to be matched exactly
input.txt The file to search

Explanation

Using grep -x extracts only lines where the entire line matches the search string. If you want to disable regular expressions, combine it with -F as grep -Fx to search for the string literally.

How to Perform an Exact Match with grep (-x Option)

Create File

cat << 'EOF' > input.txt apple orange grape apple pie Apple apple EOF

Command

grep -x "apple" input.txt

Result

apple
apple

Command

grep "apple" input.txt

Result

apple
apple pie
apple

How It Works

Item Description
grep Command that searches for a specified string
-x Displays only lines where the entire line exactly matches the search string
"apple" The string to search for
input.txt The file to search

Explanation

Since grep -x only matches when the entire line matches the search string, it excludes partial matches.
If you want to search for an exact match, using the -x option is the simplest approach.

Differences in Matching Whole Words with grep (-w Option)

Create File

cat << 'EOF' > input.txt apple pineapple apple pie Apple apple grape EOF

Command

grep "apple" input.txt

Result

apple
pineapple
apple pie
apple

Command

grep -w "apple" input.txt

Result

apple
apple pie
apple

Command

grep -iw "apple" input.txt

Result

apple
apple pie
Apple
apple

How It Works

Command Matching Method pineapple apple pie apple
grep "apple" Partial match
grep -w "apple" Whole-word exact match ×
grep -iw "apple" Case-insensitive whole-word exact match × ○ (also matches Apple)

Explanation

By default, grep searches for lines that contain the specified string.

Adding the -w option takes word boundaries into account, excluding partial matches like pineapple and extracting only lines that match the word exactly.

How to Perform an Exact Match with grep Using Line Start/End (^ and $)

Create File

cat << 'EOF' > input.txt apple orange grape apple pie pineapple apple EOF

Command

grep '^apple$' input.txt

Result

apple
apple

Command

grep -n '^apple$' input.txt

Result

1:apple
6:apple

Command

grep '^apple' input.txt

Result

apple
apple pie
apple

Command

grep 'apple$' input.txt

Result

apple
pineapple
apple

How It Works

Notation Meaning Example Matching Strings
^ Represents the start of a line ^apple apple, apple pie
$ Represents the end of a line apple$ apple, pineapple
^...$ Exact match from start to end of line ^apple$ apple only

Explanation

^ represents the start of a line and $ represents the end of a line, both regular expression anchors.
By combining them as ^string$, you can extract only lines where the entire line exactly matches the specified string.

How to Choose Between Exact Match and Regular Expressions with grep

Create File

cat << 'EOF' > input.txt apple Apple orange apple pie grape apple pineapple EOF

Command

grep -x "apple" input.txt

Result

apple
apple

Command

grep "apple" input.txt

Result

apple
apple pie
apple
pineapple

Command

grep -w "apple" input.txt

Result

apple
apple pie
apple

How It Works

Method Command What It Matches Main Use Case
Exact match grep -x "apple" input.txt Only lines where the entire line is apple Finding exactly identical strings
Word match grep -w "apple" input.txt Only apple as a whole word Excluding cases where it's part of another word
Regular expression grep "apple" input.txt All lines containing apple Partial matches or pattern searches

Explanation

grep -x only matches when the entire line matches the search string exactly.
On the other hand, regular grep treats the search term as a regular expression, so all lines containing the specified string become targets.

How to Speed Up Exact Match Search with Fixed Strings in grep (-F)

Create File

cat << 'EOF' > input.txt apple banana orange grape apple pineapple Apple banana EOF

Command

grep -F -x "apple" input.txt

Result

apple
apple

Command

grep -F -x "banana" input.txt

Result

banana
banana

Command

grep -F -x "Apple" input.txt

Result

Apple

How It Works

Item Description
-F Treats the search string as a fixed string rather than a regular expression, enabling faster searches
-x Considers it a match only when the entire line matches the search string exactly
Exact match apple matches, but pineapple does not
Speed improvement Since no regular expression parsing is performed, it is faster than regular grep on large datasets

Explanation

Using grep -F -x allows you to perform fixed-string exact match searches quickly.
For searches that don't require regular expressions, this is a simple and efficient method.

How to Search Multiple Exact Match Patterns with grep

Create File

cat << 'EOF' > input.txt apple banana orange grape apple pie banana split orange melon EOF

Command

grep -x -e "apple" -e "orange" input.txt

Result

apple
orange
orange

How It Works

Item Description
grep Command that searches text
-x Considers it a match only when the entire line matches the pattern exactly
-e "apple" Specifies an exact match pattern to search for
-e "orange" Adds a second (or later) search pattern
input.txt The file to search

Explanation

Specifying -e multiple times allows you to search for multiple patterns as an OR condition.
Combining it with -x lets you extract only data where the entire line matches, rather than partial matches.

How to Extract Lines That Do Not Exact Match with grep (-v)

Create File

cat << 'EOF' > input.txt apple orange banana apple pie Apple apple EOF

Command

grep -vx "apple" input.txt

Result

orange
banana
apple pie
Apple

How It Works

Option Description
grep Command that searches for lines matching a condition
-x Considers it a match only when the entire line matches the specified string exactly
-v Displays lines that do not match, instead of lines that do
"apple" The string used for the exact match check
input.txt The file to search

Explanation

Using -x checks for an exact match of the entire line, and combining it with -v extracts only lines that don't match exactly.
Partial matches (such as apple pie) and lines with different capitalization (Apple) are not exact matches, so they are displayed.

How to Count Exact Matches with grep (-c)

Create File

cat << 'EOF' > input.txt apple orange apple Apple apple pie apple EOF

Command

grep -c -x 'apple' input.txt

Result

3

How It Works

Option Description
grep Command that searches for lines matching a condition
-c Displays only the count of matching lines
-x Targets only lines where the entire line matches the search string exactly
'apple' The string to search for
input.txt The file to search

Explanation

Using grep -c -x lets you easily count only the lines where the entire line exactly matches the specified string.

Lines with partial matches or different capitalization, such as Apple or apple pie, are not counted.

How to Display Line Numbers of Exact Matches with grep (-n)

Create File

cat << 'EOF' > input.txt apple orange banana apple pie apple grape EOF

Command

grep -nx "apple" input.txt

Result

1:apple
5:apple

How It Works

Option Description
grep Command that searches text for lines matching a condition
-n Displays the line number of each matching line
-x Considers it a match only when the entire line matches the search string exactly
"apple" The string to search for
input.txt The file to search

Explanation

Specifying -x displays results only for lines where the entire line exactly matches the search string.

Combining it with -n also lets you check the line numbers of the matching lines at the same time.

How to Search for Exact Matches Across Multiple Files with grep

Create File

cat << 'EOF' > file1.txt apple orange banana grep EOF

Create File

cat << 'EOF' > file2.txt grape grep pineapple orange EOF

Create File

cat << 'EOF' > file3.txt grep pattern grep banana EOF

Command

grep -x "grep" file1.txt file2.txt file3.txt

Result

file1.txt:grep
file2.txt:grep
file3.txt:grep

How It Works

Item Description
grep Command that searches for a specified string
-x Displays only lines where the entire line matches the search string exactly
"grep" The string to search for as an exact match
file1.txt file2.txt file3.txt The multiple files to search

Explanation

Using grep -x displays results only when the entire line matches the search string exactly.
Specifying multiple files lets you efficiently search for exact matches across all of them at once.

How to Perform Recursive Exact Match Search with grep (-r)

Create File

mkdir -p logs

Create File

cat << 'EOF' > logs/app.log sample error warning sample sample.log Sample EOF

Create File

cat << 'EOF' > logs/system.log warning sample sample.log EOF

Command

grep -r -x "sample" logs

Result

logs/system.log:sample
logs/app.log:sample
logs/app.log:sample

How It Works

Option Description
grep Command that searches text
-r Recursively searches subdirectories
-x Displays only lines where the entire line matches the search string exactly
"sample" The string to search for as an exact match
logs The directory to search

Explanation

Using grep -r -x lets you search all files under a directory for lines where the entire line exactly matches the specified string. Partial matches or lines with different capitalization, such as sample.log or Sample, do not match.

How to Combine grep Exact Match Search with Pipes (|)

Create File

cat << 'EOF' > input.txt apple banana orange apple pie apple grape EOF

Command

cat input.txt | grep -x "apple"

Result

apple
apple

Command

cat input.txt | grep -x "apple" | wc -l

Result

2

How It Works

Item Description
cat input.txt Outputs the contents of the file to standard output
| Passes the standard output of the previous command to the next command (pipe)
grep Searches for lines matching a condition
-x Displays only lines where the entire line matches the search string exactly
"apple" The string to search for as an exact match
wc -l Counts the number of matching lines

Explanation

Using grep -x lets you extract only lines where the entire line matches the specified string.
By combining it with a pipe (|), you can pass the search results to other commands, such as for counting.

How to Check the Exit Status of a grep Exact Match Search

Create File

cat << 'EOF' > input.txt apple banana orange apple pie Apple EOF

Command

grep -x "apple" input.txt

Result

apple

Command

echo $?

Result

0

Command

grep -x "grape" input.txt

Result

no output

Command

echo $?

Result

1

How It Works

Item Description
grep -x Searches only for lines where the entire line matches the search string exactly
Exit status 0 At least one exactly matching line was found
Exit status 1 No exactly matching line was found
echo $? Displays the exit status of the previously executed grep command

Explanation

Using grep -x only produces a hit when the entire line matches the search string.

By checking the exit status with echo $?, you can easily determine a match or non-match even within shell scripts.

Causes and Solutions When grep Exact Match Search Doesn’t Work

Create File

cat << 'EOF' > input.txt apple Apple pineapple apple pie apple grape EOF

Command

grep -x "apple" input.txt

Result

apple
apple

Command

grep "apple" input.txt

Result

apple
pineapple
apple pie
apple

Command

grep -ix "apple" input.txt

Result

apple
Apple
apple

How It Works

Cause Solution
By default, grep searches for partial matches within a line, so pineapple and apple pie also match Add -x to search for an exact match of the entire line
Since matching is case-sensitive, Apple does not match apple Add -i as well and use grep -ix

Explanation

The most common cause of grep exact match searches not working as expected is that grep defaults to partial match searching.
Using -x or -ix lets you achieve the exact match search you intended.

Summary of Key Points for grep Exact Match

For grep exact match searches, it's important to choose the right options or regular expressions depending on your use case.

Understanding -x as the basics, using -w for word-level matching, and leveraging ^ and $ or regular expressions for more flexible conditions will help you work efficiently.

Once you have the basics down, you'll be able to perform accurate exact match searches in file searches and log analysis as well.

Leave a Reply

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

©︎ 2025-2026 running terminal commands