grep - dwilson2547/wiki_demo GitHub Wiki
Here’s a comprehensive grep
cheat sheet with common commands and practical examples:
grep
(Global Regular Expression Print) is a powerful Linux command for searching text and patterns in files.
grep
Cheat Sheet- 1. Basic Syntax
- 2. Common Options
- 3. Basic Searches
- 4. Advanced Searches with Regex
- 5. Practical Examples
- 6. Common Use Cases
- 7. Tips and Tricks
grep [options] "pattern" [file]
Option | Description | Example |
---|---|---|
-i |
Case-insensitive search | grep -i "error" logfile.txt |
-v |
Invert match (exclude pattern) | grep -v "success" logfile.txt |
-n |
Show line numbers | grep -n "warning" logfile.txt |
-c |
Count matching lines | grep -c "404" access.log |
-r |
Recursive search (directories) | grep -r "function" /path/to/dir/ |
-l |
List files with matches | grep -l "main()" *.c |
-w |
Match whole words only | grep -w "port" config.txt |
-A n |
Show n lines after match |
grep -A 3 "error" logfile.txt |
-B n |
Show n lines before match |
grep -B 2 "error" logfile.txt |
-C n |
Show n lines around match |
grep -C 1 "error" logfile.txt |
-E |
Extended regex (use with ` |
, +`, etc.) |
-F |
Fixed string (no regex) | grep -F "this exact string" file.txt |
--color |
Highlight matches | grep --color "pattern" file.txt |
grep "pattern" file.txt
Example:
grep "hello" example.txt
- Searches for
hello
inexample.txt
.
grep -i "pattern" file.txt
Example:
grep -i "error" logfile.txt
- Matches
error
,Error
,ERROR
, etc.
grep -c "pattern" file.txt
Example:
grep -c "404" access.log
- Counts how many times
404
appears inaccess.log
.
grep -n "pattern" file.txt
Example:
grep -n "warning" system.log
- Shows line numbers where
warning
appears.
grep -v "pattern" file.txt
Example:
grep -v "success" results.txt
- Shows lines that do not contain
success
.
grep "pattern" file1.txt file2.txt
Example:
grep "TODO" *.py
- Searches for
TODO
in all.py
files.
grep -r "pattern" /path/to/dir/
Example:
grep -r "include" /usr/include/
- Recursively searches for
include
in/usr/include/
.
grep -w "word" file.txt
Example:
grep -w "port" config.txt
- Matches
port
but notreport
orexport
.
grep -A 2 -B 2 "pattern" file.txt
Example:
grep -A 3 -B 1 "error" logfile.txt
- Shows 1 line before and 3 lines after each match.
grep "^pattern" file.txt
Example:
grep "^#" script.sh
- Finds lines starting with
#
(comments).
grep "pattern$" file.txt
Example:
grep ";$" script.sh
- Finds lines ending with
;
.
grep "[0-9]" file.txt # Lines with numbers
grep "[A-Za-z]" file.txt # Lines with letters
grep "a.*b" file.txt # Lines with `a` followed by `b`
Example:
grep "^[A-Z]" names.txt
- Finds lines starting with a capital letter.
grep -E "pattern1|pattern2" file.txt
Example:
grep -E "error|fail|warning" logfile.txt
- Matches
error
,fail
, orwarning
.
grep -E "[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+" file.txt
Example:
grep -E "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" contacts.txt
- Extracts email addresses.
grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" file.txt
Example:
grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" access.log
- Extracts IPv4 addresses.
grep -l "main()" *.c
- Lists all
.c
files containingmain()
.
zgrep "pattern" file.gz
Example:
zgrep "error" archive.log.gz
- Searches inside a
.gz
file.
command | grep "pattern"
Example:
dmesg | grep -i "usb"
- Filters
dmesg
output for USB-related messages.
ps aux | grep "nginx"
- Finds processes related to
nginx
.
grep --color "pattern" file.txt
Example:
grep --color "TODO" notes.txt
- Highlights
TODO
in the output.
grep "^$" file.txt
- Finds empty lines.
grep "^[0-9]*$" file.txt
- Matches lines with only numbers.
Task | Command |
---|---|
Search for a string | grep "text" file.txt |
Case-insensitive search | grep -i "text" file.txt |
Count matches | grep -c "pattern" file.txt |
Recursive search | grep -r "pattern" /path/ |
Exclude a pattern | grep -v "pattern" file.txt |
Show line numbers | grep -n "pattern" file.txt |
Whole word match | grep -w "word" file.txt |
Search with regex | `grep -E "pattern1 |
Search in compressed files | zgrep "pattern" file.gz |
Pipe output to grep
|
`ls -l |
-
Combine with
find
:find /var/log -type f -exec grep -l "error" {} \;
-
Use
grep
withxargs
:grep -rl "old-text" /path/ | xargs sed -i 's/old-text/new-text/g'
-
Save results to a file:
grep "pattern" file.txt > results.txt
-
Search in
less
: Press/
inless
to search interactively.