Fundamentals 10 min read

Master Linux Regex: Practical grep Examples and Patterns

This guide explains Linux regular expressions, distinguishes shell and regex meta‑characters, shows how to enable colored grep output, creates a sample text file, and provides dozens of concrete grep/egrep commands with explanations and results for common pattern‑matching tasks.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Regex: Practical grep Examples and Patterns

What is a regular expression?

A regular expression (regex) is a pattern of characters used to match specific text during search operations.

Two kinds of meta‑characters in Linux

Shell meta‑characters are interpreted by the Linux shell itself, while regex meta‑characters are interpreted by text‑processing tools such as vi, grep, sed, and awk.

Enable colored output for grep

$ alias grep='grep --color=auto'

After this alias, every match highlighted by grep will appear in color.

Create a test file

$ cat re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.

Regex meta‑characters (illustration)

Special meta‑characters (illustration)

Extended regular expressions (illustration)

Practical examples

Match lines starting with "love"

$ grep '^love' re-file
love, how much I adore you. Do you know

Match lines ending with "love"

$ grep 'love$' re-file
clover. Did you see them?  I can only hope love.

Match lines where the first character is "l", followed by any two characters, ending with "e"

$ grep 'l..e' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the

Match zero or more spaces before "love"

$ grep ' *love' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.

Case‑insensitive match for "love" or "Love"

$ grep '[Ll]ove' re-file  # case‑insensitive for the first letter
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.

Match any uppercase letter followed by "ove"

$ grep '[A-Z]ove' re-file
Lovers were all around us. It is springtime. Oh

Match lines that do not contain any uppercase letters

$ grep '[^A-Z]' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.

Match the literal string "love."

$ grep 'love\.' re-file
clover. Did you see them?  I can only hope love.

Match empty lines $ grep '^$' re-file Match any character (.*)

$ grep '.*' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.

Match the character "o" repeated 2 to 4 times

$ grep 'o\{2,4\}' re-file
groove.

Match the character "o" repeated at least 2 times

$ grep 'o\{2,\}' re-file
groove.

Match the character "o" repeated up to 2 times

$ grep 'o\{,2\}' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.

Using egrep to match one or more "o" characters

$ egrep "go+d" linux.txt
Linux is a good
god assdxw bcvnbvbjk
gooodfs awrerdxxhkl
good

Using egrep to match zero or one "o"

$ egrep "go?d" linux.txt
god assdxw bcvnbvbjk
gdsystem awxxxx

Search for multiple strings with OR

$ egrep "gd|good" linux.txt
Linux is a good
gdsystem awxxxx
good

Group filtering with alternation

$ egrep "g(la|oo)d" linux.txt
Linux is a good
glad
good

These examples demonstrate how to construct common regex patterns for line anchoring, character classes, quantifiers, alternation, and grouping, enabling powerful text searches on Linux.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Shellcommand-lineregular expressionsregex
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.