Fundamentals 9 min read

Master Linux Regex: Practical grep Examples for Powerful Text Search

This guide explains regular expressions and their metacharacters in Linux, shows how to configure grep with color, creates a sample text file, and provides numerous practical grep commands demonstrating pattern matching, character classes, quantifiers, and grouping for effective text processing.

Efficient Ops
Efficient Ops
Efficient Ops
Master Linux Regex: Practical grep Examples for Powerful Text Search

Regular expressions are character patterns used to match specific strings during search operations.

In Linux, metacharacters are divided into two categories:

Shell metacharacters, parsed by the Linux shell.

Regex metacharacters, parsed by tools such as vi, grep, sed, and awk.

To enable colored output for grep, set the alias: alias grep='grep --color=auto' Create a test file re-file with the following content:

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 Metacharacters

Special metacharacters:

Extended regular expressions:

Practical Exercises

Match all 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
...

Match "love" or "Love" (case‑insensitive for the first letter):

grep '[Ll]ove' re-file  # case‑insensitive for L
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
...

Match an uppercase letter followed by "ove":

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

Match any line containing characters outside the range A‑Z (i.e., all lowercase lines):

grep '[^A-Z]' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
...

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
...

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 at most 2 times:

grep 'o\{,2\}' re-file
I had a lovely time on our little picnic.
...

Match one or more occurrences of the preceding character:

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

Match zero or one occurrence of the preceding character:

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

Match either of multiple strings (alternation):

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
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.

text processingGrep
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

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.