copied to clipboard!
string grep

Mastering Line-End Matching with grep

updated: 2026/07/22 created: 2026/07/22

Introduction grep End of Line

grep is an essential command for text searching, but many beginners are unsure how to search based on a condition at the end of a line.

Once you understand how to specify the end of a line, you can efficiently extract things like specific file extensions or patterns at the end of log entries.

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

Reference: GNU grep

Basic grep Syntax for End of Line

Create the file

cat << 'EOF' > input.txt apple banana orange grape melon apple pie green apple banana orange juice pineapple EOF

Command to run

grep 'apple$' input.txt

Result

apple
green apple
pineapple

Command to run

grep 'juice$' input.txt

Result

orange juice

Command to run

grep 'banana$' input.txt

Result

banana
banana

How it works

Item Description
$ Anchor (metacharacter) representing the end of a line
grep '$' Searches only for lines that end with the specified string
Difference from partial match Matches only when the end of the line matches, not the middle
Example use case Used for searching file extensions or extracting lines with a specific suffix

Explanation

In grep, using $ lets you specify the end of a line.

This is useful when you only want to extract data that ends with the specified string.

grep End of Line Search for a Specific String

Create the file

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

Command to run

grep 'apple$' input.txt

Result

apple
pineapple
green apple
red apple

How it works

Item Description
Command grep 'apple$' input.txt
apple The string you want to search for.
$ In regular expressions, this represents the "end of line" and searches only for lines ending in apple.
input.txt The file to search.

Explanation

grep supports regular expressions, and by adding $ you can search only for strings that match the end of a line.
Using an end-of-line match lets you efficiently extract only lines with a specific suffix.

Combining grep End of Line and Start of Line for an Exact Match

Create the file

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

Command to run

grep '^apple$' input.txt

Result

apple
apple

How it works

Pattern Meaning
^ Represents the start of a line
apple The string you want to search for
$ Represents the end of a line
^apple$ Searches only for lines where the entire content, from start to end, is exactly apple

Explanation

By combining start of line (^) and end of line ($) as in grep '^apple$', you can search only for lines whose entire content matches the string exactly.
Lines like apple pie or pineapple, which merely contain the string, will not match.

How to Search Multiple grep End of Line Patterns

Create the file

cat << 'EOF' > input.txt error warning info error.log warning.log info.log server-error server-warning server-info backup-error backup-warning backup-info EOF

Command to run

grep -E '(error|warning)$' input.txt

Result

error
warning
server-error
server-warning
backup-error
backup-warning

How it works

Item Description
grep Command that searches text for lines matching a condition
-E Enables extended regular expressions, allowing you to use |
(error|warning) Matches either error or warning
$ Represents the end of a line, matching only lines that end with the specified string

Explanation

Multiple end-of-line patterns can be searched in a single grep call by combining extended regular expressions with the OR operator (|).

Adding $ ensures a match only occurs when the specified string is at the end of the line.

Using grep to Find Lines Ending in Digits, Letters, or Symbols

Create the file

cat << 'EOF' > input.txt apple banana123 orange! grape? melon. peach- kiwi_ lemon9 cat@ dog# bird$ fish% EOF

Command to run

grep '[0-9]$' input.txt

Result

banana123
lemon9

Command to run

grep '[[:alpha:]]$' input.txt

Result

apple

Command to run

grep '[[:punct:]]$' input.txt

Result

orange!
grape?
melon.
peach-
kiwi_
cat@
dog#
bird$
fish%

How it works

Item Description
$ Anchor representing the end of a line
[0-9] Matches a single digit from 0-9
[[:alpha:]] Matches a single letter (A-Z, a-z)
[[:punct:]] Matches a single symbol character (! @ # $ % . ? - etc.)
grep '[pattern]$' Searches only for lines whose end matches the specified character class

Explanation

$ is the regular expression anchor representing the end of a line.
By combining it with character classes, you can easily extract lines that end in digits, letters, or symbols.

How to Use grep to Find Trailing Whitespace at the End of a Line

Create the file

cat << 'EOF' > input.txt normal line space at end tab at end multiple spaces at end no trailing whitespace EOF

Command to run

grep -nE '[[:blank:]]+$' input.txt

Result

2:space at end 
3:tab at end	
4:multiple spaces at end    

How it works

Pattern Meaning
grep Command that searches text
-n Displays the line number of the match
-E Uses extended regular expressions
[[:blank:]] Matches a half-width space or a tab
+ Indicates the preceding character repeats one or more times
$ Represents the end of a line, so only trailing whitespace is matched

Explanation

Using [[:blank:]]+$ lets you detect only the spaces or tabs at the end of a line.
This regular expression is often used to check for unwanted trailing whitespace.

How to Use grep to Find Blank Lines

Create the file

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

Command to run

grep '$' input.txt

Result

apple

banana
grape

orange
melon

Command to run

grep -n '^$' input.txt

Result

2:
5:
8:

How it works

Pattern Meaning Use
$ Matches the end of a line Matches every line (including blank lines)
^ Matches the start of a line Represents the beginning of a line
^$ End of line immediately after start of line Matches only blank lines with no characters at all
-n Displays line numbers Lets you check which line numbers are blank

Explanation

To search for blank lines with grep, use ^$, which combines the start of line (^) and end of line ($) with nothing in between.

Adding the -n option lets you confirm not just the blank lines themselves, but also the line numbers where they occur.

Using the grep -E Option for End of Line Search

Create the file

cat << 'EOF' > input.txt apple banana orange grape apple.txt banana.txt orange.log grape.csv README EOF

Command to run

grep -E '.txt$' input.txt

Result

apple.txt
banana.txt

Command to run

grep -E 'e$' input.txt

Result

apple
orange
grape

How it works

Item Description
grep Command that searches for lines matching a condition
-E Option that enables extended regular expressions
$ Metacharacter representing the end of a line
\.txt$ Searches for lines ending in .txt (the . is escaped as \.)
e$ Searches for lines that end with the letter e

Explanation

grep -E lets you use extended regular expressions, and $ lets you search only for strings that match the end of a line.
This is commonly used to extract file extensions or lines with a particular ending.

How to Search grep End of Line Across Multiple Files

Create the file

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

Create the file

cat << 'EOF' > file2.txt melon banana pineapple orange EOF

Create the file

cat << 'EOF' > file3.txt strawberry apple kiwi banana EOF

Command to run

grep 'banana$' file1.txt file2.txt file3.txt

Result

file1.txt:banana
file2.txt:banana
file3.txt:banana

Command to run

grep 'orange$' file1.txt file2.txt file3.txt

Result

file1.txt:orange
file2.txt:orange

Command to run

grep -H 'apple$' file1.txt file2.txt file3.txt

Result

file1.txt:apple
file2.txt:pineapple
file3.txt:apple

How it works

Item Description
grep Command that searches for a specified pattern
banana$ $ represents the end of a line, so only lines ending in banana match
file1.txt file2.txt file3.txt Searches multiple files at once
-H Always shows the filename before a matched line

Explanation

By adding $ to the end of the search string, grep matches only strings found at the end of a line.
When you specify multiple files, you can search all of them for matching lines in a single execution.

How to Specify an End of Line Condition in a Recursive grep Search

Create the file

mkdir -p sample/{dir1,dir2}

Create the file

cat << 'EOF' > sample/dir1/file1.txt error error log warning EOF

Create the file

cat << 'EOF' > sample/dir2/file2.txt error fatal error error.log EOF

Command to run

grep -r 'error$' sample

Result

sample/dir2/file2.txt:error
sample/dir2/file2.txt:fatal error
sample/dir1/file1.txt:error

How it works

Item Description
grep Command that searches text
-r Searches a directory recursively
error$ $ represents the end-of-line anchor, matching only lines that end with "error"
Non-matching examples error log and error.log do not match because the line does not end in error

Explanation

Using the regular expression $ in grep lets you search based on the end of a line.

Combined with -r, you can efficiently search every file in a directory tree for lines matching your end-of-line condition.

A Practical Example of grep End of Line Search in a Log File

Create the file

cat << 'EOF' > input.txt 2025-01-10 10:00:01 INFO Application started 2025-01-10 10:01:15 ERROR Database connection failed 2025-01-10 10:02:30 INFO User login success 2025-01-10 10:03:45 ERROR Disk full 2025-01-10 10:04:50 INFO Backup completed EOF

Command to run

grep 'failed$' input.txt

Result

2025-01-10 10:01:15 ERROR Database connection failed

Command to run

grep 'full$' input.txt

Result

2025-01-10 10:03:45 ERROR Disk full

Command to run

grep 'success$' input.txt

Result

2025-01-10 10:02:30 INFO User login success

How it works

Item Description
grep Command that searches for lines matching the specified pattern
$ Regular expression metacharacter representing "end of line"
failed$ Searches only for lines ending in failed
full$ Searches only for lines ending in full
success$ Searches only for lines ending in success

Explanation

Using the regular expression $ in grep matches a string only when it occurs at the end of a line.
This is convenient when you want to extract status messages or error messages found at the end of a log file.

Causes and Solutions When grep End of Line Search Does Not Work

Create the file

cat << 'EOF' > input.txt apple apple banana orange apple$ EOF

Command to run

grep "apple$" input.txt

Result

apple

Command to run

grep -E "apple$" input.txt

Result

apple

Command to run

grep '$$' input.txt

Result

apple$

Command to run

grep -n "apple " input.txt

Result

2:apple 

How it works

Description Command Behavior
End of line specifier $ Matches only when the pattern is found at the very end of the line
$ as a literal character \$$ Escapes $ so it is searched for as a literal character
Basic regular expressions grep Interprets regex symbols such as $
Extended regular expressions grep -E Allows access to more regex features
Trailing whitespace grep "apple " Invisible whitespace characters also count as part of the match condition

Explanation

When a grep end-of-line search doesn't behave as expected, the main cause is usually that $ is being interpreted as the regular-expression symbol for "end of line."

Also, if there is trailing whitespace or a special character at the end of a line, what you see and what is actually in the string can differ, causing the search results to be off.

Commonly Used grep Options for End of Line Searches

Create the file

cat << 'EOF' > input.txt error error error.log test error EOF

Command to run

grep 'error$' input.txt

Result

error
test error

Command to run

grep -n 'error$' input.txt

Result

1:error
4:test error

Command to run

grep -E 'error$' input.txt

Result

error
test error

Command to run

grep -i 'ERROR$' input.txt

Result

error
test error

Command to run

grep -v 'error$' input.txt

Result

error 
error.log

How it works

Command / Option Role How it works for end-of-line search
grep '$' End-of-line match search $ specifies the terminating position of the string
-n Show line numbers Lets you confirm the position of each match
-E Extended regular expressions Easier to combine with more complex regular expressions
-i Ignore case Treats ERROR$ and error$ as the same
-v Inverted match Retrieves lines that do NOT match the end-of-line condition

Explanation

An end-of-line search in grep uses the regular expression $.
Since $ means "the end of the line," it is often used in log analysis and configuration-file searches to extract lines ending in a specific character or string.

Summary of grep End of Line Points

Once you grasp the basics of regular expressions, grep end-of-line searches can be applied to a wide range of situations.

After understanding the basic syntax, using exact matches, multiple conditions, the -E option, and multi-file searches as needed can greatly improve your efficiency.

Once you have mastered what's covered here, you'll be able to confidently apply it to a variety of text and log analysis tasks.

Leave a Reply

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

©︎ 2025-2026 running terminal commands