Fundamentals 6 min read

Master grep: Powerful Regex Search Techniques for Linux

This guide explains how to use the Linux grep command with common options and regular‑expression patterns, providing step‑by‑step examples that demonstrate counting matches, case‑insensitive searches, inverse matches, anchoring, character classes, and quantifiers.

ITPUB
ITPUB
ITPUB
Master grep: Powerful Regex Search Techniques for Linux

The grep command in Linux is a powerful text‑search utility that uses regular expressions to locate patterns and print matching lines. Its name stands for Global Regular Expression Print and it is available to all users.

Syntax and Common Options

Basic usage: grep [options] pattern [file...] Key options include: -c: output only the count of matching lines. -i: ignore case distinctions. -h: suppress file name output when searching multiple files. -l: list only the names of files containing matches. -n: show matching lines with their line numbers. -s: suppress error messages for nonexistent or non‑matching files. -v: invert the match, showing lines that do not match. --color=auto: highlight the matching part of the line.

Practical Examples

1) Find lines containing the word png:

[linuxmi@linux:~/linuxmi]$ grep -n 'png' linuxmi.py

2) Find lines that do not contain png:

[linuxmi@linux:~/linuxmi]$ grep -vn 'png' linuxmi.py

3) Search for strings where na is not preceded by v:

[linuxmi@linux:~/linuxmi]$ grep -n '[^v]na' linuxmi.py

4) Search for strings where na is not preceded by any lowercase letter:

[linuxmi@linux:~/linuxmi]$ grep -n '[^a-z]na' linuxmi.py

5) Use ^ to match lines that start with a specific pattern, e.g., lines beginning with ba:

[linuxmi@linux:~/linuxmi]$ grep -n '^ba' linuxmi.py

6) Use [^] to match lines that do not start with a letter:

[linuxmi@linux:~/linuxmi]$ grep -n '^[^a-zA-Z]' linuxmi.py

7) Use $ to match lines ending with a specific character, e.g., lines ending with a period:

[linuxmi@linux:~/linuxmi]$ grep -n '\.$' linuxmi.py

Note: the dot . is a special regex character and must be escaped with a backslash to match a literal period.

8) Use . to match any single character (except newline). Find lines where l and k are separated by exactly two characters:

[linuxmi@linux:~/linuxmi]$ grep -n 'l..k' linuxmi.py

9) Find lines containing consecutive e characters:

[linuxmi@linux:~/linuxmi]$ grep -n 'eee*' linuxmi.py

Here * means “zero or more” repetitions of the preceding character.

10) Find lines that start with l, end with e, and contain at least one x in between:

[linuxmi@linux:~/linuxmi]$ grep -n 'lxx*e' linuxmi.py

11) Find lines that start with l and end with k, with any characters (or none) in between:

[linuxmi@linux:~/linuxmi]$ grep -n 'l.*k' linuxmi.py

12) Use {n} to specify an exact number of repetitions. To match lines containing exactly two e characters:

[linuxmi@linux:~/linuxmi]$ grep -n 'e\{2\}' linuxmi.py

Curly braces are special characters and must be escaped with a backslash.

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.

LinuxShellregexGrepcommand-linetext search
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.