Quiz Entry - updated: 2026.07.14
How do you use grep with regular expressions?
grep PATTERN file prints lines matching the pattern; key flags are -i (ignore case), -v (invert), -n (line numbers), -r (recurse), -E (extended regex).
The name comes from the old ed editor command g/re/p — "globally search for a regular expression and print." Each flag bends its behaviour: -v flips it to "show lines that don't match" (great for filtering out comments), -c counts instead of printing, -r walks a directory tree. Chain them with pipes for quick log triage.
Common options:
| Option | Meaning |
|---|---|
-i |
Case insensitive |
-v |
Invert match (show non-matching) |
-n |
Show line numbers |
-c |
Count matches |
-E |
Extended regex |
-r |
Recursive search |
Examples:
# Find "error" (case insensitive)
grep -i "error" logfile.txt
# Show lines NOT matching pattern
grep -v "^#" config.file
# Remove comments and empty lines
grep -v "^[#;]" file.txt | grep -v "^$"
# Count matches
grep -c "pattern" file.txt
# Search recursively in directory
grep -r "TODO" /project/src/
Filter /etc/ethertypes (remove comments):
grep -v '^[#;]' /etc/ethertypes
Go deeper:
grep(1) man page (man7.org) —
-i -v -n -c -r -Eand the BRE/ERE behaviour.