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.
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.py2) Find lines that do not contain png:
[linuxmi@linux:~/linuxmi]$ grep -vn 'png' linuxmi.py3) Search for strings where na is not preceded by v:
[linuxmi@linux:~/linuxmi]$ grep -n '[^v]na' linuxmi.py4) Search for strings where na is not preceded by any lowercase letter:
[linuxmi@linux:~/linuxmi]$ grep -n '[^a-z]na' linuxmi.py5) Use ^ to match lines that start with a specific pattern, e.g., lines beginning with ba:
[linuxmi@linux:~/linuxmi]$ grep -n '^ba' linuxmi.py6) Use [^] to match lines that do not start with a letter:
[linuxmi@linux:~/linuxmi]$ grep -n '^[^a-zA-Z]' linuxmi.py7) Use $ to match lines ending with a specific character, e.g., lines ending with a period:
[linuxmi@linux:~/linuxmi]$ grep -n '\.$' linuxmi.pyNote: 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.py9) Find lines containing consecutive e characters:
[linuxmi@linux:~/linuxmi]$ grep -n 'eee*' linuxmi.pyHere * 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.py11) 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.py12) 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.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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
