Fundamentals 6 min read

Mastering grep: Essential Regex Search Techniques for Linux

This guide explains the powerful Linux grep command, its syntax and common options, and provides step‑by‑step examples demonstrating how to use regular expressions for precise text searching and filtering in shell environments.

Open Source Linux
Open Source Linux
Open Source Linux
Mastering grep: Essential Regex Search Techniques for Linux

Introduction

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

1. Syntax and Options

Basic usage: grep [options] 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: display matching lines with line numbers. -s: suppress error messages for nonexistent or non‑matching files. -v: invert match to show lines that do not match. --color=auto: highlight matching text.

2. Practical Examples

1) Find lines containing "png"

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

2) Find lines not containing "png"

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

3) Find strings where the character before "na" is not "v"

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

4) Find strings where the character before "na" is not a lowercase letter

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

5) Match lines starting with "ba"

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

6) Match lines not starting with a letter

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

7) Match lines ending with a period

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

Note: The dot character "." has special meaning and must be escaped with a backslash to match a literal period.

8) Use "." to match any character (except newline)

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

9) Find lines containing consecutive "e" characters

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

The asterisk "*" means zero or more repetitions of the preceding character.

10) Find lines starting with "l" and ending with "e" with at least one "x" in between

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

11) Find lines starting with "l" and ending with "k" with any characters in between

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

12) Use "{n}" to match exactly n occurrences (e.g., two "e" characters)

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

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

grep example
grep example
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
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.