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.
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.py2) Find lines not containing "png"
[linuxmi@linux:~/linuxmi]$ grep -vn 'png' linuxmi.py3) Find strings where the character before "na" is not "v"
[linuxmi@linux:~/linuxmi]$ grep -n '[^v]na' linuxmi.py4) Find strings where the character before "na" is not a lowercase letter
[linuxmi@linux:~/linuxmi]$ grep -n '[^a-z]na' linuxmi.py5) Match lines starting with "ba"
[linuxmi@linux:~/linuxmi]$ grep -n '^ba' linuxmi.py6) Match lines not starting with a letter
[linuxmi@linux:~/linuxmi]$ grep -n '^[^a-zA-Z]' linuxmi.py7) Match lines ending with a period
[linuxmi@linux:~/linuxmi]$ grep -n '\.$' linuxmi.pyNote: 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.py9) Find lines containing consecutive "e" characters
[linuxmi@linux:~/linuxmi]$ grep -n 'eee*' linuxmi.pyThe 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.py11) Find lines starting with "l" and ending with "k" with any characters in between
[linuxmi@linux:~/linuxmi]$ grep -n 'l.*k' linuxmi.py12) Use "{n}" to match exactly n occurrences (e.g., two "e" characters)
[linuxmi@linux:~/linuxmi]$ grep -n 'e\{2\}' linuxmi.pyCurly braces are special characters and must be escaped with a backslash.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
