Fundamentals 15 min read

Mastering grep: Powerful Search Techniques and Regular Expressions

This guide explains how to use the Unix grep family—including grep, egrep, and fgrep—to perform global text searches with regular expressions, showing common options, color highlighting, recursive searches, character classes, anchors, quantifiers, and extended patterns through clear examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering grep: Powerful Search Techniques and Regular Expressions

Introduction

grep (global search regular expression and print) is a powerful text‑search tool that uses regular expressions to find and print matching lines. The Unix grep family includes grep , egrep (extended grep) and fgrep (fixed‑string grep). GNU grep on Linux adds the -G, -E and -F options to invoke egrep and fgrep functionality.

Common grep options

# grep [-acinv] [--color=auto] 'search_string' filename
-a

: treat binary files as text -c: count matching lines -i: ignore case -n: show line numbers -v: invert match (show non‑matching lines) --color=auto: highlight matches

Basic examples

# grep root /etc/passwd
# grep -n root /etc/passwd
# grep -v root /etc/passwd
# grep -v root /etc/passwd | grep -v nologin

Color alias

Add the following line to ~/.bashrc and source it to always enable colored output:

alias grep='grep --color=auto'
source ~/.bashrc

Recursive search

# grep 'energywise' *               # search current directory
# grep -r 'energywise' *            # search current directory and sub‑directories
# grep -l -r 'energywise' *         # list only matching file names

Character classes

Search for t[ae]st to match "test" or "taste": # grep -n 't[ae]st' regular_express.txt Use [^g]oo to find "oo" not preceded by "g": # grep -n '[^g]oo' regular_express.txt Range examples: [0-9] for digits, ^[a-z] for lines starting with a lowercase letter, ^[^a-zA-Z] for lines not starting with a letter, \.$ for lines ending with a period.

Anchors and quantifiers

Use ^the to match lines beginning with "the" and \.$ for lines ending with a dot. The dot . matches any single character, and * repeats the preceding element zero or more times.

# grep -n 'g..d' regular_express.txt   # matches g??d where ?? are any two characters
# grep -n 'ooo*' regular_express.txt   # matches oo, ooo, oooo, …
# grep -n 'goo*g' regular_express.txt  # matches gog, goog, gooog, …
# grep -n 'g.*g' regular_express.txt   # matches any line containing g … g

Quantifier ranges

Use o\{2\} to match exactly two o's, go\{2,5\}g for g followed by 2‑5 o's and a final g, or go\{2,\}g for g followed by at least two o's.

# grep -n 'o\{2\}' regular_express.txt
# grep -n 'go\{2,5\}g' regular_express.txt
# grep -n 'go\{2,\}g' regular_express.txt

Extended grep (egrep)

egrep adds extra metacharacters. Example: find lines containing "NW" or "EA". # egrep 'NW|EA' testfile Search for one or more 3's: # egrep '3+' testfile Search for a digit optionally followed by a decimal point and another digit:

# egrep '2\.?[0-9]' testfile

Fixed‑string search (fgrep)

fgrep is faster but only matches literal strings.

# fgrep '*' /etc/profile
# grep -F '*' /etc/profile
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.

command-lineregular expressionsGreptext search
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.