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:

<code>alias grep='grep --color=auto'</code>

Create a test file

re-file

with the following content:

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

Regex Metacharacters

Special metacharacters:

Extended regular expressions:

Practical Exercises

Match all lines starting with "love":

<code>grep '^love' re-file
love, how much I adore you. Do you know</code>

Match lines ending with "love":

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

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

<code>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</code>

Match zero or more spaces before "love":

<code>grep ' *love' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
...</code>

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

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

Match an uppercase letter followed by "ove":

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

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

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

Match the literal string "love.":

<code>grep 'love\.' re-file
clover. Did you see them?  I can only hope love.</code>

Match empty lines:

<code>grep '^$' re-file</code>

Match any character (.*):

<code>grep '.*' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
...</code>

Match the character "o" repeated 2 to 4 times:

<code>grep 'o\{2,4\}' re-file
groove.</code>

Match the character "o" repeated at least 2 times:

<code>grep 'o\{2,\}' re-file
groove.</code>

Match the character "o" repeated at most 2 times:

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

Match one or more occurrences of the preceding character:

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

Match zero or one occurrence of the preceding character:

<code>egrep "go?d" linux.txt
god assdxw bcvnbvbjk
gdsystem awxxxx</code>

Match either of multiple strings (alternation):

<code>egrep "gd|good" linux.txt
Linux is a good
gdsystem awxxxx
good</code>

Group filtering with alternation:

<code>egrep "g(la|oo)d" linux.txt
Linux is a good
glad
good</code>
LinuxShellregextext 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

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