Mastering grep: Powerful Search Techniques and Regex Tips for Linux
This guide explains how to use the versatile grep command for global text searches, covering basic options, color highlighting, alias setup, recursive searches, and detailed regular‑expression techniques such as character classes, anchors, quantifiers, and extended patterns.
Introduction
grep (global regular expression print) is a powerful text‑search tool that uses regular expressions to find matching lines and print them.
The Unix grep family includes grep, egrep, and fgrep. egrep adds support for more regex metacharacters, while fgrep treats the pattern as a fixed string. Linux typically uses the GNU version, which provides the -G, -E, and -F options to emulate grep, egrep, and fgrep respectively.
Common grep usage
# grep [-acinv] [--color=auto] 'search_string' filenameOptions: -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.
Examples
# grep root /etc/passwd # cat /etc/passwd | grep root # grep -n root /etc/passwd # grep -v root /etc/passwd # grep -v root /etc/passwd | grep -v nologinColor alias
To avoid typing --color=auto each time, add the following alias to ~/.bashrc and reload:
alias grep='grep --color=auto'
source ~/.bashrcRecursive search
# grep 'energywise' * # grep -r 'energywise' * # grep -l -r 'energywise' *The -r option searches directories recursively, and -l lists only matching file names.
grep and regular expressions
Character classes
Search for t[ae]st to match both test and taste :
# grep -n 't[ae]st' regular_express.txtNegated character class
Find lines containing oo not preceded by g:
# grep -n '[^g]oo' regular_express.txtRanges
Match lowercase letters at the start of a line: # grep -n '^[a-z]' regular_express.txt Match lines that do not start with a letter:
# grep -n '^[^a-zA-Z]' regular_express.txtAnchors
Match lines beginning with the: # grep -n '^the' regular_express.txt Match lines ending with a period:
# grep -n '\.$' regular_express.txtWildcard and repetition
Match any four‑character string starting with g and ending with d: # grep -n 'g..d' regular_express.txt Match lines containing at least two consecutive o characters: # grep -n 'ooo*' regular_express.txt Match strings that start and end with g with any characters in between:
# grep -n 'g.*g' regular_express.txtQuantifiers with braces
Find exactly two o characters: # grep -n 'o\{2\}' regular_express.txt Find between two and five o characters after a g and before another g:
# grep -n 'go\{2,5\}g' regular_express.txtExtended grep (egrep)
Search for lines containing NW or EA: # egrep 'NW|EA' testfile Search for one or more 3 characters: # egrep '3+' testfile Search for lines with zero or one decimal point followed by a digit 0‑9: # egrep '2\.?[0-9]' testfile Search for one or more consecutive no strings:
# egrep '(no)+' testfileFixed‑string search (fgrep)
Search for a literal asterisk character: # fgrep '*' /etc/profile or using grep -F:
# grep -F '*' /etc/profileSigned-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.
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.
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.
